jQuery POST JSON 数据到 .asmx 网络服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5320802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:57:31  来源:igfitidea点击:

POST JSON data to .asmx webservice

jqueryjsonrestasmx

提问by swandog

I'm trying to post some simple parameters to a .asmx webservice.
I get the following error: Request format is invalid: application/json; charset=utf-8.
What I really need to get to is to be able to pass a complex object, but I can't get past making a POST request with json content type.

我正在尝试将一些简单的参数发布到 .asmx 网络服务。
我收到以下错误:请求格式无效:application/json; 字符集=utf-8。
我真正需要的是能够传递一个复杂的对象,但我无法通过使用 json 内容类型发出 POST 请求。

Here is my WebService Definition

这是我的 WebService 定义

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public int JsonTest2(int myparm1, int myparm2)
{
    return 101;
}

And this is my javascript code

这是我的 javascript 代码

function JsonTest2() {
    $.ajax({
        type: 'POST',
        url: "http://localhost/WebServices/MyTest.asmx/JsonTest2",
        data: "{myparm1:105,myparm2:23}",
        contentType: 'application/json; charset=UTF-8',
        dataType: 'json',
        async: false,
        success: function (msg) {
            alert(msg);
        },
        error: function (msg) {
            alert('failure');
            alert(msg);
        }
    });
}

回答by Dave Ward

Make sure your ASMX service class is decorated with the [ScriptService]attribute.

确保您的 ASMX 服务类使用该[ScriptService]属性进行修饰。

回答by Oleg

You should use as datathe value which formatted as correct JSON data:

您应该使用作为data正确 JSON 数据格式的值:

{"myparm1":105,"myparm2":23}

instead of

代替

{myparm1:105,myparm2:23}

You can validate on the site http://www.jsonlint.com/which data are JSON data. So you should change your code to

您可以在站点http://www.jsonlint.com/上验证哪些数据是 JSON 数据。所以你应该把你的代码改成

$.ajax({
    type: 'POST',
    url: "http://localhost/WebServices/MyTest.asmx/JsonTest2",
    data: '{"myparm1":105,"myparm2":23}',
    contentType: 'application/json; charset=UTF-8',
    dataType: 'json',
    async: false,
    success: function (msg) {
        alert(msg.d);
    },
    error: function (msg) {
        alert('failure');
        alert(msg);
    }
});

In case of more complex input parameters I recommend you to use JSON.stringifyfunctionfrom the json2.js(see this answerfor example):

如果输入参数更复杂,我建议您使用json2.js 中的JSON.stringify函数(例如,请参阅此答案):

var myValue1 = 105, myValue2 = 23;
$.ajax({
    type: 'POST',
    data: JSON.stringify({myparm1:myValue1, myparm2:myValue2}),
    ...
});

In the last version of $.ajaxusage the myValue1and myValue2can be complex structures (objects with properties) or arrays having even another complex structures or arrays as the properties.

在使用的最后一个版本中$.ajaxmyValue1myValue2可以是复杂结构(具有属性的对象)或具有甚至另一个复杂结构或数组作为属性的数组。

回答by Arun Manjhi

Make sure the URL contains port number when using localhost.

使用 localhost 时,请确保 URL 包含端口号。

  url: "http://localhost:1297/WebServices/MyTest.asmx/JsonTest2",