【问题标题】:Sending data to ASP.NET through jQuery Ajax() gives error通过 jQuery Ajax() 向 ASP.NET 发送数据会出错
【发布时间】:2017-05-16 19:44:28
【问题描述】:

我正在尝试使用 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 脚本中放置一个断点时,它显示没有收到值,它给了我这个错误:

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

我做错了什么?

【问题讨论】:

    标签: javascript c# jquery asp.net ajax


    【解决方案1】:

    添加:

    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)
      });
    

    【讨论】:

    • 哇,谢谢这个修复它!没想到会这么简单。
    猜你喜欢
    • 1970-01-01
    • 2015-10-27
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多