通过jQuery Ajax()向ASP.NET发送数据会产生错误

时间:2017-05-16 19:44:28

标签: javascript c# jquery asp.net ajax

我正在尝试使用以下代码将数据发送到带有jQuery的ASP.NET程序:

$.ajax({
    method: "POST",
    dataType: 'json',
    url: "http://localhost:52930/api/person/",
    data: JSON.stringify({Name: "Sinan", Password: 'test'})
})
    .done(function( msg ) {
        alert(msg)
    });

标题信息显示数据已发送。但是当我在asp.net脚本中放置一个断点时,它显示了收到的值并且它给了我这个错误:

enter image description here

这是来自jQuery请求的标头信息:

enter image description here

我做错了什么?

1 个答案:

答案 0 :(得分:2)

添加:

contentType: "application/json; charset=utf-8",

method更改为type

这样的事情:

$.ajax({
    type: "POST",
    dataType: 'json',
    url: "http://localhost:52930/api/person/",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({
      Name: "Sinan",
      Password: 'test'
    })
  })
  .done(function(msg) {
    alert(msg)
  });