开发者

post function of jquery

I am running this code :

$.post('https://graph.facebook.com/me',{},
     function(msg){
 开发者_开发问答    console.log( "Data Saved: " + msg );
    }
);  

and I am only getting Data Saved : as output in firebug nothig else is coming and call to the url is also not showing why ?


You can't post to another domain (or protocol, or port). More specifically, you can but you can't see the response that comes back for security reasons. This is part of the same origin policy browsers implement to keep your data from being posted/exploited on remote domains that aren't the same as the page you went to.

Picture for example I loaded http://www.yoursite.com and it tried to repeatedly post (as me remember) to https://www.mybank.com, using my stored cookies, etc...you see how you really wouldn't want that to happen...that's why it's disallowed and the response you get back will be null instead of actually seeing the content.

In this case you're looking for one of their API calls using JSONP (callback=? on the URL in jQuery), which works by creating a <script> tag...an entirely different thing altogether. In your case you're looking for something like this:

$.getJSON('https://graph.facebook.com/me?callback=?', function(msg){
   console.log(msg);
});

Though, that's not a valid API call by itself (e.g. you need an access token at least), you'll need to use the method you're actually looking for instead.


Bit of a tangent, but since this is so misunderstood, let's take another more popular example to illustrate the dangers if this was allowed. Think about all those services you stay logged into, Facebook, Twitter, etc. This is another facet of the same origin policy, not allowing your cookies, etc to be used is you do try and post...why is this? If my page could just post to Facebook or Twitter already logged in as you, man I could easily broadcast whatever message I wanted...you can see how this would be useful....and immediately used for evil as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜