服务器不支持JSONP时的跨域请求 [英] Cross-Domain request when server does NOT support JSONP
问题描述
我试图解决这个问题几个小时。
我得到了一个 wikia网络,我想从东西,但它只支持json,甚至不是xml或jsonp。
我试过一切。如果我调用它作为jsonp我的客户端返回错误语法错误:无效标签导致服务器不会返回它作为jsonp或json在所有。
如果我添加callback =?
这里是我的代码的一部分(我修改了太多次,没有什么工作到目前为止)
$('#searchBox')。keydown(function(){
$ .ajax({
type: GET,
url:http://leagueoflegends.wikia.com/index.php?callback=?,
data:{
action:ajax,
rs:getLinkSuggest,
format:json,
query:jungl
},
success:function(json){
alert成功);
},
错误:function(){
alert(Error);
},
dataType:json
});
});
UPDATE
这里是我的解决方案, b $ b(使用YQL)var url =http://leagueoflegends.wikia.com/index.php?action = ajax& rs = getLinkSuggest& format = json& query = jung
url ='http://query.yahooapis.com/v1/public/yql?q='+ encodeURIComponent('select * from html其中url ='+ url +'')+'& format = xml';
$ .ajax({
type:GET,
url:url,
success:function(text){
text = text.split(< ; p>)[1] .split(< / p>)[0];
alert(text);
} $ b alert(Error);
},
dataType:text
});
解决方案如果远程服务器不支持JSONP或允许CORS,则您必须退回请求服务器端绕过同源规则。
Im trying to solve this problem for hours now. I got a wikia web which I want to get stuff from but it supports only json, not even xml or jsonp.
I tried everything. If I call it as jsonp my client returns error "SyntaxError: invalid label" cause the server doesnt return it as jsonp or json at all. If I add "callback=?" to the url and leave it as json, same error returned.
Here is part of my code (I modified it too many times and nothing worked so far)
$('#searchBox').keydown(function() { $.ajax({ type: "GET", url: "http://leagueoflegends.wikia.com/index.php?callback=?", data: { action: "ajax", rs: "getLinkSuggest", format: "json", query: "jungl" }, success: function(json){ alert("Success"); }, error: function(){ alert("Error"); }, dataType: "json" }); });
UPDATE Here is my solution, which works: (Using YQL)
var url = "http://leagueoflegends.wikia.com/index.php?action=ajax&rs=getLinkSuggest&format=json&query=jung" url = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + url + '"') + '&format=xml'; $.ajax({ type: "GET", url: url, success: function(text){ text = text.split("<p>")[1].split("</p>")[0]; alert(text); }, error: function(){ alert("Error"); }, dataType: "text" });
解决方案If the remote server doesn't support JSONP or allow CORS, then you'll have to bounce the request server-side to bypass the same-origin rule.
这篇关于服务器不支持JSONP时的跨域请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!