我想让任何域都能访问Web服务,所以我研究了以下内容
的 clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
的的crossdomain.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<?xml version="1.0" ?>
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type"/>
<allow-access-from domain="*" />
</cross-domain-policy>
在我的web.config中,我添加了以下内容
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
我一个接一个地使用了两个文件并同时使用了这两个文件。仍然无法从其他域请求 这是jquery代码
var cid="My String Input";
var webMethod = "MyWemMethodUrl";
var parameters = "{'ContactID':'" + cid + "'}";
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
debugger;
var _JSONObj = jQuery.parseJSON(result.d);
if (_JSONObj.StatusCode == "0") {
alert("Error");
}
else {
alert("Success");
}
},
error: function (jqXHR, exception, thrownError) {
debugger;
if (jqXHR.status === 0) {
alert('Not connected.\nPlease verify your network connection.');
} else if (jqXHR.status == 404) {
alert('The requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
总是jqXHR.status = 0路径出错 甚至我尝试了dataType:“jsonp”instread of dataType:“json” 这是我的控制台屏幕来自Chrome浏览器
答案 0 :(得分:2)
好的,最后我发现我的代码出了什么问题。
我在javascript Ajax调用中在url中添加回调,并将数据类型值设置为jsonp
$.ajax({
type: "POST",
url: WebMethod+'?callback=jsonCallback',
crossDomain: true,
contentType: "application/json; charset=utf-8",
data: parameters,
dataType: "jsonp",
jsonpCallback: 'jsonCallback',
success: function (result) {
/*SUCCESS CODE*/
},
error: function (jqXHR, exception, thrownError) {
/*error Code*/
}
});
而不是字符串返回类型函数,我改为使用void的非返回类型。
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void WebMethod()
{
String ReponseResult = "";/*Json String that i want to return*/
/*My Code and logic
----
*/
/*To return Json String I added following Code*/
try
{
string callback = HttpContext.Current.Request.Params["callback"];
string json = ReponseResult;
string response1 = string.IsNullOrEmpty(callback) ? json : string.Format("{0}({1});", callback, json);
// Response
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write(response1);
}
catch (Exception ex) { }
}