Gra*_*ant 32 javascript ajax asp.net-mvc jquery
我将两个字符串参数从jQuery ajax调用传递给MVC控制器方法,期待json响应.我可以看到参数填充在客户端,但服务器端的匹配参数为空.
这是javascript:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "List/AddItem",
data: "{ ListID: '1', ItemName: 'test' }",
dataType: "json",
success: function(response) { alert("item added"); },
error: function(xhr, ajaxOptions, thrownError) { alert(xhr.responseText); }
});
Run Code Online (Sandbox Code Playgroud)
这是控制器方法:
Function AddItem(ByVal ListID As String, ByVal ItemName As String) As JsonResult
'code removed for brevity
'ListID is nothing and ItemName is nothing upon arrival.
return nothing
End Function
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Luk*_*Led 32
我试过了:
<input id="btnTest" type="button" value="button" />
<script type="text/javascript">
$(document).ready( function() {
$('#btnTest').click( function() {
$.ajax({
type: "POST",
url: "/Login/Test",
data: { ListID: '1', ItemName: 'test' },
dataType: "json",
success: function(response) { alert(response); },
error: function(xhr, ajaxOptions, thrownError) { alert(xhr.responseText); }
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
和C#:
[HttpPost]
public ActionResult Test(string ListID, string ItemName)
{
return Content(ListID + " " + ItemName);
}
Run Code Online (Sandbox Code Playgroud)
有效.删除contentType并设置data不带双引号.
| 归档时间: |
|
| 查看次数: |
106016 次 |
| 最近记录: |