我在网上搜索了一些,似乎无法解决这个问题。如何强制响应只是JSON?
web.config
<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>
AJAX
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>");
}
web服务
' 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>
答案 0 :(得分:0)
使用.asmx
您需要使用WCF
网络服务以json格式返回数据时,无法使用JSON。