ajax跨域访问 webservice
前端代码
$.ajax({
type: "POST",
url: "http://localhost:9767/WebService1.asmx/HelloWorld?jsoncallback=?",
data: "{}",
dataType: "jsonp",
success: function (data) {
alert(data.result);
}
});
webservice 的代码
public void HelloWorld()
{
string callbackMethodName = HttpContext.Current.Request.Params["jsoncallback"] ?? "";
HttpContext.Current.Response.Write(callbackMethodName + "({result:'Hello World'})");
HttpContext.Current.Response.End();
}
这样会得到

但是如果用这样的代码
如果url不加?jsoncallback=?
$(function () {
$.ajax({
type: "POST",
url: "http://localhost:9767/WebService1.asmx/HelloWorld",
data: "{}",
dataType: "jsonp",
success: function (data) {
alert(data.result);
}
});
public string HelloWorld()
{
return "Hello World";
}
则会返回一个xml (
<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">Hello World</string>
) 并且会不去回调函数 这是怎么回事,笔者对webservice 不是很了解 也是刚刚接触 有知道的能告诉下笔者 笔者会万分感谢

浙公网安备 33010602011771号