Cli*_*ead 4 jquery json web-services response.contenttype
我想使用ajax和jquery将表单发布到.asmx webservice,并将Webservice中的值作为JSON返回.
我正在使用ASP.NET 4.0.我知道为了从Web服务返回JSON,需要设置以下内容:(1)dataType:"json"(2)contentType:"application/json; charset = utf-8",(3)type:"POST" (4)将数据设置为某物.我测试了这个并且它工作正常(即我的webservice将数据作为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 contenttype),那么webservice将返回XML.
非常感谢任何见解:)
如果您将使用WFC RESTfull Service而不是.asmx webservice,则可以从您的问题中实现所有要求.但是使用.asmx webservice和JSON作为输出需要至少使用contentType: 'application/json'.在不同的地方你可以找到一个原因 - 安全原因(见JSON劫持).
可能'x-www-form-urlencoded'不是你真正的问题.如果使用dataType: "json"参数也会以"test1 = value1&test2 = value2"的形式发送!!! 唯一的区别是,所有值都应该是JSON编码的.并且jQuery 不会对数据进行JSON编码.(你可以查看jQuery的代码.)主要的区别只是"Accept:application/json"将在请求头中显式设置.
看看我最近写的jget ajax调用httpget webmethod(c#)不能正常工作.在这篇文章中被问到了GET请求的例子.但它几乎是一样的.唯一的区别是,编码后的数据将附加到URL(用于GET请求).对于POST请求,数据将以正文发送.顺便说一下如果设置"processData:false"(参见http://api.jquery.com/jQuery.ajax/),GET数据也将在body体内发送.所以请阅读我的代码示例,从JQuery ajax调用到httpget webmethod(c#)不起作用.我希望你能收到如何实现自己想法的想法.
我认为你的.asmx webservice调用的复杂数据编码存在问题.以下是"复杂"数据的示例:
[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 } }
};
}Run Code Online (Sandbox Code Playgroud)
哪里
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; }
}Run Code Online (Sandbox Code Playgroud)
并在客户端
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);
}
}
});Run Code Online (Sandbox Code Playgroud)
($.toJSON来自JSON插件)如果需要,您可以将此示例修改为HTTP POST.
还有一个小建议.如果您使用jQuery 1.4.x,可以尝试使用'none'作为dataType.请参阅文档:"如果没有指定,jQuery将智能地尝试获取结果,基于响应的MIME类型(XML MIME类型将产生XML,在1.4 JSON中将产生一个JavaScript对象,在1.4脚本中将执行脚本,其他任何东西将作为字符串返回)"
最好的祝福
| 归档时间: |
|
| 查看次数: |
20726 次 |
| 最近记录: |