将 JSON 数据发布到 .asmx Web 服务

发布于 2024-10-22 08:54:47 字数 852 浏览 4 评论 0 原文

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

这是我的 WebService 定义

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

这是我的 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);
        }
    });
}

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.

Here is my WebService Definition

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

And this is my javascript code

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对风讲故事 2024-10-29 08:54:48

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

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

Make sure the URL contains port number when using localhost.

  url: "http://localhost:1297/WebServices/MyTest.asmx/JsonTest2",
只是一片海 2024-10-29 08:54:47

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

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

一江春梦 2024-10-29 08:54:47

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

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

而不是

{myparm1:105,myparm2:23}

You can validate on the site 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);
    }
});

如果输入参数更复杂,我建议您使用 JSON.stringify 函数rel="nofollow noreferrer">json2.js(请参阅例如这个答案):

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

$.ajax 的最新版本中,myValue1myValue2 可以是复杂的结构(对象具有属性)或具有甚至另一个复杂结构或数组作为属性的数组。

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

{"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

$.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.stringify functionfrom the json2.js (see this answer for example):

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

In the last version of $.ajax usage the myValue1 and myValue2 can be complex structures (objects with properties) or arrays having even another complex structures or arrays as the properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文