站点A c# webserver 代码
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json,XmlSerializeString = false)]
public void Login(string userName, string userPwd, string macAddress)
{
var response = HttpContext.Current.Response;
response.ContentType = "text/json";
var ws = ValitateLogin(userName, userPwd, macAddress);
var json = new JSONHelper {success = true};
json.AddItem("UnReadEmailNum", ws.UnReadEmailNum.ToString());
json.AddItem("IsValid", ws.IsValid.ToString());
json.AddItem("RetString", ws.RetString);
json.AddItem("EmailUrl",ws.EmailUrl);
json.ItemOk();
response.Write(json.ToString());
}
站点B ajax 跨域调用代码
$(function () {
$("#jsonp_btn").click(function () {
$.ajax({
async:false,
url: 'http://192.168.1.2:14671/OA/Email.asmx/Login', // 跨域URL
type: 'GET',
dataType: 'jsonp',
contentType: "application/json; charset=utf-8",
jsonp: 'jsoncallback', //默认callback
data: {"userName":"admin","userPwd":"123456","macAddress":""}, //请求数据
timeout: 5000,
beforeSend: function(){
//jsonp 方式此方法不被触发。原因可能是dataType如果指定为jsonp的话,就已经不是ajax事件了
},
success: function(json) {