[英]How Can I get the data from one domain to another domain using ajax jsonp?
例如:我的網站網址為"http://localhost:54887/CustomOrdering.html"
但我想從另一個網站"http://localhost:27746/Orders.aspx"
獲取數據。為此,我在CustomOrdering.html
寫道
function SessionLogin() {
$.ajax({
type: "GET",
dataType: "jsonp",
contentType: "application/json",
async: false,
url: 'http://localhost:27746/Orders.aspx/SessionLogin',
success: function (msg) {
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
在Orders.aspx中
public void SessionLogin()
{
HttpContext.Current.Response.ContentType = "application/json";
string qs = HttpContext.Current.Request.QueryString["callback"];
HttpContext.Current.Response.Write(qs+ "( [{ \"x\": 10, \"y\": 15}] )");
}
我收到錯誤jQuery11110002214477863162756_1449484451326 was not called
在控制台中顯示錯誤消息“未捕獲的SyntaxError:意外的令牌<”,然后單擊它指向Orders.aspx設計頁面開始” 錯誤圖像
另一個服務器需要使用給定的回調名稱包裝返回值:
public void SessionLogin()
{
HttpContext.Current.Response.ContentType = "application/json";
string qs = HttpContext.Current.Request.QueryString["callback"];
HttpContext.Current.Response.Write( qs + "( [{ \"x\": 10, \"y\": 15}] )");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.