只有jQuery的Ajax请求在Firefox [英] jquery ajax request works only in firefox
问题描述
我有以下的HTML,警报仅在Mozilla Firefox中,当我点击button.Why?
< HTML>
< HEAD>
<脚本类型=文/ JavaScript的SRC =jquery.js和>< / SCRIPT>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
$(按钮)。点击(函数(){
$阿贾克斯(网址:{url:https://graph.facebook.com/1524623057/
成功:函数(){
警报(1);
}});
});});
< / SCRIPT>
< /头>
<身体GT;
<按钮>发送请求< /按钮>
< /身体GT;
< / HTML>
来自Facebook的结果是不正确的JSON,因为它换行和标签 - 也是,它的服务为text / javascript的。
Facebook的支持JSONP,但是,这样你就可以这样做,而不是:
$。阿贾克斯({
网址:https://graph.facebook.com/1524623057/,
输入:'得到',
数据类型:JSONP
})。完成(功能(数据){
// 数据!
});
这将基本钉在了?回调= jQuery23423234234或任何随机ID生成给Facebook,返回一个可以调用的函数。
如果你想自己解析它,这样做:
告诉 $。阿贾克斯
使用类型文本,如:
$。阿贾克斯({
网址:https://graph.facebook.com/1524623057/,
数据类型:文本
})
然后再清理。下面是关于清理那种JS对SO答案,这样你就可以使用具有把它扔在一个新的功能或eval'ing它 $。parseJSON
代替。 <一href="http://stackoverflow.com/questions/5791356/converting-multiline-indented-json-to-single-line-using-javascript">Converting多行缩进JSON来使用JavaScript单行
这样的话,您可以 VAR数据= $ .parseJSON(cleanedUpJsonFromFacebook)
并访问对象的属性。
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>
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.
这篇关于只有jQuery的Ajax请求在Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!