我在网上做了一些搜索,似乎无法弄清楚这一点。如何强制响应只是 JSON?
网络配置
<httpHandlers>
<remove path="*.asmx" verb="*" />
<add path="*.asmx" verb="*" validate="false"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=3.5.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</httpHandlers>
阿贾克斯
function get_data() {
var objdata = new Object;
objdata.tool = '1';
objdata.product = 'Something';
objdata.details = '9';
objdata.bogus = 'should not pass validation';
var server = 'http://crossdomainserver:87';
var webMethod = '/LogInfo.asmx/LogInfo?';
var url = server + webMethod + "tool=" + objdata.tool + "&" +
"product=" + objdata.product + "&" +
"details=" + objdata.details + "&" +
"bogus=" + objdata.bogus;
$('#url').html("<p>" + url + "</p>");
$.ajax({
url: url,
cache: false,
dataType: "jsonp",
contentType: "application/json; charset=utf-8",
success: function (data) {
$('#get').html("<p>" + data + "</p>");
},
error: function (e) {
}
});
$('#result').html("<p>" + result.statusText + "</p>");
}
网络服务
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class LogInfo
Inherits System.Web.Services.WebService
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function LogInfo() As String
'do database/error logic here
Return jsonreturn.ToString
End Function
End Class
回复
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">{"tool":"1","product":"Something","details":"9","user":"username","systime":"1/29/2013 10:20:50 AM","site":"Somewhere, AZ","team":"000000000","result":"1"}</string>