nav*_*een 1 vb.net asp.net ajax response-headers
我的ASMX电话正在返回 Content-Type = text/xml; charset=utf-8
我正在使用ASP.NET 3.5和jQuery.Ajax
.
根据Stack Overflow的众多建议添加了这些内容
我做过这些事
在web.config上完成此操作
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
Run Code Online (Sandbox Code Playgroud)在ASMX内部的函数调用中,我正在使用它们
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function GetCompanies(SearchedCompany As String) As String
Run Code Online (Sandbox Code Playgroud)jQuery Ajax调用是这样的
var searchString = "SearchedCompany=" + $(".auto-company").val();
$.ajax({
type: "POST",
url: "Services/CompaniesService.asmx/GetCompanies",
data: searchString,
dataType: 'json',
success: function (msg) {
companies = msg.hasOwnProperty("d") ? msg.d : msg;
},
error: function (xhr, status, error) {
}
});
Run Code Online (Sandbox Code Playgroud)但无论我做什么,响应头仍然是xml类型.我在这里错过了什么?
免责声明:我知道这里已多次询问过这个问题.但无法找到任何答案.如您所见,我已经实现了所提供的所有解决方案.
您还需要将contentType设置为"application/json; charset = utf-8"
http://encosia.com/using-jquery-to-consume-aspnet-json-web-services/
因为jQuery确实在具有数据的POST请求上设置了内容类型,所以内容类型最初设置为默认的"application/x-www-form-urlencoded".然后在IE7中,setRequestHeader添加了"application/json; charset = utf-8"而不是真正设置它.因此,内容类型对于JSON序列化是不正确的,并且Web服务返回XML而不是JSON.
归档时间: |
|
查看次数: |
2399 次 |
最近记录: |