I have the following html, alert works only in mozilla firefox when I click the button.Why?
我有下面的html,只有当我点击按钮时,alert才会在mozilla firefox中工作。
<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>
2 个解决方案
#1
2
The result from facebook isn't proper json, as it has line breaks and tabs - also, it's served as text/javascript.
facebook的结果不是合适的json,因为它有换行符和制表符——同样,它被用作文本/javascript。
Facebook supports jsonp, however, so you could do this instead:
然而,Facebook支持jsonp,所以你可以这样做:
$.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.
这将基本上附加在?callback= jquery234234234或任何生成到facebook的随机id上,返回可调用的函数。
If you want to parse it yourself, do:
如果您想自己解析它,请这样做:
Tell $.ajax
to use type 'text', e.g.
告诉美元。使用“text”类型的ajax。
$.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
然后把它清理干净。这里有一个关于清除这类js的答案,所以您可以使用$。使用parseJSON而不是将其放入一个新函数或对其进行eval。使用javascript将多行、缩进的json转换为单行
That way, you can var data = $.parseJSON( cleanedUpJsonFromFacebook )
and access the object properties.
这样,就可以将var数据= $。parseJSON(cleanedUpJsonFromFacebook)并访问对象属性。
#2
2
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.
看起来该请求返回一个JSON数据类型。当代码接收到结果而不识别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.
此外,success回调使用一个响应参数,您可以在取回JSON时使用该参数检查JSON。
Check out http://api.jquery.com/jQuery.ajax/
看看http://api.jquery.com/jQuery.ajax/
#1
2
The result from facebook isn't proper json, as it has line breaks and tabs - also, it's served as text/javascript.
facebook的结果不是合适的json,因为它有换行符和制表符——同样,它被用作文本/javascript。
Facebook supports jsonp, however, so you could do this instead:
然而,Facebook支持jsonp,所以你可以这样做:
$.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.
这将基本上附加在?callback= jquery234234234或任何生成到facebook的随机id上,返回可调用的函数。
If you want to parse it yourself, do:
如果您想自己解析它,请这样做:
Tell $.ajax
to use type 'text', e.g.
告诉美元。使用“text”类型的ajax。
$.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
然后把它清理干净。这里有一个关于清除这类js的答案,所以您可以使用$。使用parseJSON而不是将其放入一个新函数或对其进行eval。使用javascript将多行、缩进的json转换为单行
That way, you can var data = $.parseJSON( cleanedUpJsonFromFacebook )
and access the object properties.
这样,就可以将var数据= $。parseJSON(cleanedUpJsonFromFacebook)并访问对象属性。
#2
2
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.
看起来该请求返回一个JSON数据类型。当代码接收到结果而不识别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.
此外,success回调使用一个响应参数,您可以在取回JSON时使用该参数检查JSON。
Check out http://api.jquery.com/jQuery.ajax/
看看http://api.jquery.com/jQuery.ajax/