将JSON数据发布到.asmx Web服务 [英] POST JSON data to .asmx webservice

查看:149
本文介绍了将JSON数据发布到.asmx Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的Web服务定义

Here is my WebService Definition

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

这是我的JavaScript代码

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

推荐答案

确保您的ASMX服务类已用[ScriptService]属性修饰.

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

这篇关于将JSON数据发布到.asmx Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆