问题而作出POST从JQuery的一个Web API问题、POST、JQuery、Web

2023-09-10 19:29:51 作者:永远太远

我有一个Web API具有以下POST方法

I have a web api with the following POST Method

public HttpResponseMessage Post([FromBody]string package)

我有一个使用的HttpClient 没有问题一个控制台应用程序。当我尝试拨打电话按的jQuery 的手段,我得到的包变量。

I have a console app that uses the HttpCLient with no problems. When I try to make a call by means of jQuery, I get null on the package variable.

这是在code我现在:

This is the code I have right now:

$.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);

        }
    });

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

The "message" variable is a complex model containing two properties.

我能是做错了?

我会AP preciate你的帮助......

I'll appreciate your help...

推荐答案

我能找到这里另一篇文章的解决办法:Calling网络阿比POST始终临危从jQuery的的空值。

I was able to find a solution on another article here: Calling Web Api POST always recieves null value from jQuery.

我不得不修改我的code以下方式:

I had to modify my code the following way:

这行:数据:JSON.stringify(消息),更改为:数据:JSON.stringify('='+消息),。

我去了API post方法,并添加下面一行code:

I went to the API post method and added the following line of code:

包= package.Replace(=,);

为了去除'='被加在JQuery的帖子。

in order to remove the '=' that was added on the JQuery post.

 
精彩推荐
图片推荐