从 JQuery 向 Web API 进行 POST 时出现问题[重复]

2024-04-06

我有一个带有以下 POST 方法的 Web api

public HttpResponseMessage Post([FromBody]string package)

我有一个控制台应用程序,使用HttpCLient没有问题。当我尝试通过以下方式拨打电话时jQuery, I get null在包变量上。

这是我现在的代码:

$.ajax({
        url: 'http://localhost:8081/api/Package/',
        type: 'POST',
        data: JSON.stringify(message),       
    contentType: "application/json;charset=utf-8",
        success: function (data) {
            alert(data.length);

        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert('Status: '+xhr.status+', Error Thrown: '+thrownError);

        }
    });

“消息”变量是一个包含两个属性的复杂模型。

我可能做错了什么?

我会很感激你的帮助...


将值绑定到发送到服务器的响应时可能存在问题

尝试将数据发送为

$.ajax({
        url: 'http://localhost:8081/api/Package/',
        type: 'POST',
        data: { package : JSON.stringify(message) }        
        datatype: 'json',
        success: function (data) {
            alert(data.length);

        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert('Status: '+xhr.status+', Error Thrown: '+thrownError);

        }
    });

我假设你的 JSON.stringify(message) 返回一个字符串值

updated

 public HttpResponseMessage Post([ModelBinder]string package)

允许包从任何地方绑定而不仅仅是身体为我做到了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 JQuery 向 Web API 进行 POST 时出现问题[重复] 的相关文章

随机推荐