2013-10-17 7 views
8

いつも「エラー」アラートが表示され、何が間違っているかわかりません。私はちょうど私が送る文字列( "testexpression")を取得しようとしています。パラメタがなければ、それはデータ部分と何かでなければなりません。ここで jquery ajaxでデータを渡す

はjqueryの一部です:

<script> 

$("#meaning").blur(function() { 

    $.ajax({ 
     type: "POST", 
     url: '/GetMeaning/', 
     data: {"expression" : "testexpression"}, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: successFunc, 
     error: errorFunc 
    }); 

    function successFunc(data, status) { 
     $("#dictionaryDropDown").html(data); 
    } 

    function errorFunc() { 
     alert('error'); 
    } 
}) 
</script> 

そして、これはコントローラです:

public class GetMeaningController : Controller 
{ 
    // 
    // GET: /GetMeaning/ 

    [HttpGet] 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Index(string expression) 
    { 

     return Json(expression, JsonRequestBehavior.AllowGet); 

    } 

} 

(更新:タイプはポストですが、私はちょうど同様に取得して、それを試して、私ました

答えて

12

文字列/ jsonとしてデータを送信する必要があります。あなたはjavascriptオブジェクトを送信しています。また、URLは、あなたが実際にJSONを返送したいだけではなく、いくつかの仮定すると、私は

return Json(
    new { this.expression = expression }, 
    JsonRequestBehavior.AllowGet); 

をお勧めします、絶対URLとバックエンド側ではない相対URL

$("#meaning").blur(function() { 

    $.ajax({ 
     type: "POST", 
     url: '/GetMeaning/', 
     data: JSON.stringify({expression: "testexpression"}), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: successFunc, 
     error: errorFunc 
    }); 

    function successFunc(data, status) { 
     $("#dictionaryDropDown").html(data); 
    } 

    function errorFunc() { 
     alert('error'); 
    } 
}) 
0

する必要がありますランダムな文字列。

関連する問題