Chr*_*isP 4 javascript vb.net asp.net json
我有以下测试方法:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<System.Web.Script.Services.ScriptService()> _
Public Class DemoService
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function GetCustomer() As String
Return "Microsoft"
End Function
End Class
Run Code Online (Sandbox Code Playgroud)
即使添加了ResponseFormat属性,响应仍然以XML而不是JSON的形式返回.
思想赞赏.
我对这个问题有点迟了,但希望这可以帮助你或其他任何人在以后绊倒这个帖子.
您绝对可以使用ASMX服务以JSON进行通信.
您的服务代码看起来不错.既然你没有表现出你如何称呼它,我敢打赌这就是你的问题所在.从ASMX"ScriptServices"中获取JSON的一个要求是必须使用正确的内容类型标头调用它们,并且必须使用POST请求.斯科特格思里有一篇关于这些要求背后推理的好文章.
因此,如果您只是DemoService.asmx/GetCustomer在浏览器中请求,那么您将获得XML.但是,如果您使用application/json内容类型发出POST请求,您将获得与JSON序列化相同的结果.
如果你正在使用jQuery,这里有一个如何在客户端请求你的DemoService的例子:
$.ajax({
type: "POST",
contentType: "application/json",
url: "DemoService.asmx/GetCustomer",
data: "{}",
dataType: "json",
success: function(response) {
// Prints your "Microsoft" response to the browser console.
console.log(response.d);
}
});
Run Code Online (Sandbox Code Playgroud)
更多信息:http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
| 归档时间: |
|
| 查看次数: |
31864 次 |
| 最近记录: |