如何调用外部URL jQuery的? [英] How to call external url in jquery?

查看:172
本文介绍了如何调用外部URL jQuery的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery把意见在Facebook墙上。

但我的ajax调用不alowing外部URL。

谁能解释我们如何能够利用外部URL与jQuery?

下面是我的code:

  VAR fbUrl =htt​​ps://graph.facebook.com/16453004404_481759124404/comments?access_token=my_token;

$阿贾克斯({
    网址:fbURL,
    数据:消息=+ commentdata,
    键入:POST,
    成功:函数(RESP){
        警报(RESP);
    },
    错误:函数(E){
        警报('错误:'+ E);
    }
});
 

其捐赠xmlhttt prequest错误。

解决方案

所有这些答案都是错的!

就像我在我的评论说,之所以你是因为URL失败同源策略,但你仍然可以在我们的AJAX功能来打另一个域,请参见尼克Cravers这个类似的问题回答:

  

您需要触发JSONP行为   用$ .getJSON()通过加入放大器;回调=?   在查询字符串,像这样的:

<$p$p><$c$c>$.getJSON("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&titles="+title+"&format=json&callback=?", 功能(数据){     doSomethingWith(数据); });

您可以在这里进行测试。

     

如果不使用JSONP你打的   同源策略是阻止   从得到任何XmlHtt prequest   数据了。

考虑到这一点,后续的code应该工作:

  VAR fbURL =htt​​ps://graph.facebook.com/16453004404_481759124404/comments?access_token=my_token;

$阿贾克斯({
    网址:fbURL +&放大器;回调=?,
    数据:消息=+ commentdata,
    键入:POST,
    成功:函数(RESP){
        警报(RESP);
    },
    错误:函数(E){
        警报('错误:'+ E);
    }
});
 

I am trying to put comments on Facebook wall using jquery.

But my ajax call not alowing external url .

can anyone explain how can we use external url with jquery ?

below is my code :

var fbUrl="https://graph.facebook.com/16453004404_481759124404/comments?access_token=my_token";

$.ajax({        
    url: fbURL ,
    data: "message="+commentdata,
    type: 'POST',
    success: function (resp) {
        alert(resp);
    },
    error: function(e){
        alert('Error: '+e);
    }  
});

its giving xmlhtttprequest error.

解决方案

All of these answers are wrong!

Like I said in my comment, the reason you're getting that error because the URL fails the "Same origin policy", but you can still us the AJAX function to hit another domain, see Nick Cravers answer on this similar question:

You need to trigger JSONP behavior with $.getJSON() by adding &callback=? on the querystring, like this:

$.getJSON("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&titles="+title+"&format=json&callback=?",
function(data) {
    doSomethingWith(data); 
}); 

You can test it here.

Without using JSONP you're hitting the same-origin policy which is blocking the XmlHttpRequest from getting any data back.

With this in mind, the follow code should work:

var fbURL="https://graph.facebook.com/16453004404_481759124404/comments?access_token=my_token";

$.ajax({
    url: fbURL+"&callback=?",
    data: "message="+commentdata,
    type: 'POST',
    success: function (resp) {
        alert(resp);
    },
    error: function(e) {
        alert('Error: '+e);
    }  
});

这篇关于如何调用外部URL jQuery的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆