JS调用.net webservice存在跨域问题。调用方式如下:
JS前台:
var url = "http://localhost:4263/zbhjjcWeb/Service.asmx/HelloWord";
var str = "v1=‘123‘&v2=‘Boston‘&jsonpCallback=HelloWord"; //参数传递,其中v1和v2为.net webservice中函数参数,jsonpCallback为.net webservice中的函数
jQuery.ajax(url, {
type: "GET", //你选择get或者post最后都是get,跨域情况下都是get
contentType: "application/json",
data: str, //这里是要传递的参数
dataType: "jsonp",
jsonp: ‘callback‘, //服务器端的回调函数名
// jsonpCallback: ‘HelloWord‘, //回调函数名
success: function (result) { //回调函数,result,返回值
alert(result.value1 + ";" + result.value2);
},
error: function (err) { alert(err.textStatus); }
});
webservice代码:
[WebMethod]
public void HelloWord(String v1,String v2)
{
string callback = Context.Request["callback"];
string response = "{\"value1\":\"" + v1 + "\",\"value2\":\"" + v2 + "\"}";
string call = callback + "(" + response + ")";
Context.Response.Write(call);
Context.Response.End();
}
原文:http://blog.csdn.net/shileimohan/article/details/46237095