JQuery的Ajax跨域请求的解决方案

时间:2021-08-25 03:07:33

客户端调用代码示例:

var myurl = "http://js.yingdoo.com/embed/CAPTCHA.ashx?m=" + phone_val + "";
$.ajax({
type: "get",
async: false,
url: myurl,
dataType: "jsonp",
jsonp: "cb",
jsonpCallback: "sj",
success: function (json) {
$(".mytel").val(json[0].mycd);
},
error: function () {
alert('fail');
}
});

$(".sr_inp_p").show();
}

});

服务端返回数据的示例代码:
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
String callbackFunName = context.Request["cb"];
context.Response.Write(callbackFunName + "([ { mycd:\"wahaha\"}])");
}