2017-12-20 10 views
0

AJAXの成功を打っていない次は、AJAX呼び出し元である:、私は500エラーを取得していますし、

function editItem(id) { 

      $.ajax({ 
       type: 'GET', 
       url: '/bookmarkrest/edititem?Id=' + id, 
       success: function (json) { 
        //alert('Success.'); 
        window.location.href = json.redirectUrl; 
       }, 
       error: function() { 
        alert("No Change."); 
       } 
      }); 
     }; 

次がありますメソッドと呼ばれる。 EditLink方法が異なるページ(指定ページ)に私を取る必要があります。

 public ActionResult EditItem(int? Id = 0) 
    { 
     try 
     { 
      Bookmark bookmark = repository.GetBookmark(Id); 


      if (bookmark is Link) 
      { 
       return Json(new { redirectUrl = Url.Action("EditLink", "BookmarkREST", new { Id = Id }) }); 
      } 
      return RedirectToAction("Index", "Home"); 

     } 
     catch (Exception) 
     { 
      return RedirectToAction("Index", "Home"); 
     } 

    } 

    [HttpGet] 
    public ActionResult EditLink(int? id) 
    { 

     if (id == null) 
     { 
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
     } 
     Link link = repository.GetLink(id); 

     if (link == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(new LinkViewModel { name = link.Name, uri = link.URI }); 

    } 

enter image description here

を私はまた、クローム開発ツールのネットワーク要求応答]タブで、正しい応答を取得しています: enter image description here

URLを入力すると、正しいページにデータでアクセスします。 enter image description here

なぜ私はサムに保管されているのかわかりませんeページ、助けてもらえますか?

+4

をAJAXの全体のポイントは、同じページに滞在することですので。ちょうど他のページに行きたい場合、あなたが望むのはリンクです。 – David

+0

通常のアンカーを使用するか、リダイレクトを自分で行う必要があります。 'window.location = url' – Jasen

+0

2行のコードを追加しました.1つはAjaxで成功し、もう1つはEditItemメソッドでJSONを返します。私は500エラーが発生します。 – naz786

答えて

0

AjaxリクエストがPOSTとdatatype属性でなければなりませんでしたが含ま:

function editItem(id) { 

      $.ajax({ 
       type: 'POST', 
       datatype: 'JSON', 
       url: '/bookmarkrest/edititem?Id=' + id, 
       success: function (json) { 
        //alert('Success.'); 
        window.location.href = json.redirectUrl; 
       }, 
       error: function (json) { 
        alert(json.message); 
       } 
      }); 
     }; 
関連する問題