小编典典

如果ContentType不是JSON,我可以从.asmx Web服务返回JSON吗?

json

我想使用ajax和jquery将表单发布到.asmx Web服务,并将Web服务的值作为JSON返回。

我正在使用ASP.NET 4.0。我知道,为了从Web服务返回JSON,需要设置以下内容(1)dataType:“
json”(2)contentType:“ application / json; charset = utf-8”,(3)类型:“ POST”
(4)将数据设置为某物。我已经对此进行了测试,并且 如果所有“四个” 都已设置** ,则可以正常工作(即,我的Web服务将数据作为JSON返回)。

但是,在我的情况下,我想做一个标准的表格,即test1 = value1&test2 =
value2,所以contentType不是JSON,但我想返回JSON {test1:value1}。这似乎不起作用,因为contentType是“
application / x-www-form-urlencoded ”而不是“ application / json; charset =
utf-8
”。

谁能告诉我为什么我不能这样做?对于我来说,必须显式发送JSON以取回JSON似乎很疯狂,但是,如果您不使用JSON(即发布urlencoded内容类型),则Web服务将返回XML。

任何见解都将不胜感激:)


阅读 250

收藏
2020-07-27

共1个答案

小编典典

如果您将使用WFC RESTfull服务而不是.asmx Web服务,则可以实现问题中的所有要求。但是,将.asmx
Web服务与JSON作为输出配合使用时,至少 需要 使用contentType: 'application/json'。您可以在其他地方找到一个原因-安全原因(请参阅JSON劫持)。

可能“ x-www-form-urlencoded”不是您的真正问题。如果使用dataType: "json"参数,还将以“ test1 =
value1&test2 = value2”的形式发送!!!唯一的区别是,所有值都应使用JSON编码。jQuery 不会对数据进行JSON编码
。(您可以查看jQuery代码。)主要区别只是将在请求标头中显式设置“ Accept:application / json”。

看一下我最近写的对httpgetwebmethod(c#)的JQueryajax调用不起作用。在此帖子中,有人询问了GET请求的示例。但这几乎是相同的。唯一的区别是,编码后的 数据
将被附加到URL(用于GET请求)。对于POST请求, 数据 将在正文中发送。顺便说一句,如果设置了一个“
processData:false”(请参阅http://api.jquery.com/jQuery.ajax/),则GET数据也将在主体内部发送。因此,从对httpgetwebmethod(c#)的JQueryajax调用中读取我的代码示例不起作用。我希望您能收到想法,以实现自己的目标。

我认为您在为.asmx网络服务调用编码复杂数据时遇到问题。这是带有“复杂”数据的示例:

[WebMethod]
[ScriptMethod (UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public OutputData AjaxGetMore (InputData input) {
    return new OutputData () {
        id = input.id,
        message = new List { "it's work!", "OK!" },
        myInt = new int[] { input.myInt[0] + 1, input.myInt[1] + 1, 20, 75 },
        myComplexData = new InternalData () { blaBla = "haha", iii = new int[] { 1, 2, 3, 4, 5 } }
    };
}

哪里

public class InternalData {
    public string blaBla { get; set; }
    public int[] iii { get; set; }
}
public class OutputData {
    public string id { get; set; }
    public List message { get; set; }
    public int[] myInt { get; set; }
    public InternalData myComplexData { get; set; }
}
public class InputData {
    public string id { get; set; }
    public int[] myInt { get; set; }
    public InternalData data { get; set; }
}

在客户端

var myData = { id: "li1234", myInt: [100, 200], data : {blaBla: "Hahhh!", iii: [10,20,30]}}
var myDataForjQuery = {input:$.toJSON(myData)};
$.ajax({
    type: "GET",
    url: "/Service1.asmx/AjaxGetMore", // + idAsJson,
    data: myDataForjQuery, // idAsJson, //myData,
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(msg) {
        // var msg = {__type: "Testportal.outputData", id: "li1234", message: "it's work!", myInt:101}
        alert("message=" + msg.d.message + ", id=" + msg.d.id + ", myInt=" + msg.d.myInt); 
    },
    error: function(res, status) {
        if (status ==="error") {
            // errorMessage can be an object with 3 string properties: ExceptionType, Message and StackTrace
            var errorMessage = $.parseJSON(res.responseText);
            alert(errorMessage.Message);
        }
    }
});

$.toJSON来自JSON插件)您可以根据需要将此示例修改为HTTP POST。

还有一个小建议。如果您使用jQuery 1.4.x,则可以尝试使用“
none”作为dataType。请参阅文档:“如果未指定任何内容,则jQuery将根据响应的MIME类型智能地尝试获取结果(XMLMIME类型将生成XML,在1.4JSON中将生成JavaScript对象,在1.4脚本中将执行脚本,其他任何内容都将以字符串形式返回)”

最好的祝福

2020-07-27