Reputation: 4185
I have the following html, alert works only in mozilla firefox when I click the button.Why?
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.ajax({url:"https://graph.facebook.com/1524623057/",
success:function(){
alert(1);
}});
});});
</script>
</head>
<body>
<button>send request</button>
</body>
</html>
Upvotes: 1
Views: 957
Reputation: 11068
The result from facebook isn't proper json, as it has line breaks and tabs - also, it's served as text/javascript.
Facebook supports jsonp, however, so you could do this instead:
$.ajax({
url: 'https://graph.facebook.com/1524623057/',
type: 'get',
dataType: 'jsonp'
}).done( function( data ){
// the data!
});
This will basically tack on the ?callback=jQuery23423234234 or whatever random id is generated to facebook, returning a function that can be called.
If you want to parse it yourself, do:
Tell $.ajax
to use type 'text', e.g.
$.ajax({
url: 'https://graph.facebook.com/1524623057/',
dataType: 'text'
})
and then clean it up. Here's an answer on SO about cleaning up that kind of js, so you can use $.parseJSON
instead of having to throw it in a new function or eval'ing it. Converting multiline, indented json to single line using javascript
That way, you can var data = $.parseJSON( cleanedUpJsonFromFacebook )
and access the object properties.
Upvotes: 2
Reputation: 17703
It looks like that request returns a JSON data type. The code is complaining about a colon when it receives the result and doesn't recognize the JSON.
Try this:
$.ajax("https://graph.facebook.com/1524623057/",
{
success:function(){
alert(1);
},
dataType: 'json'
}
);
Also, the success callback takes a response parameter that you can use to inspect the JSON when you get it back.
Check out http://api.jquery.com/jQuery.ajax/
Upvotes: 2