JSON在FireFox&上返回空响应Safari(Windows Vista) [英] JSON return empty response on FireFox & Safari (Windows Vista)

查看:133
本文介绍了JSON在FireFox&上返回空响应Safari(Windows Vista)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已解决:

问题出在Firefox 6.0.2安全性之内. 我已将网址请求从以下位置更改: http://mysite.com/ajax/request 到/ajax/request 及其工作.

The problem lies within Firefox 6.0.2 security. I have changed my URL request from: http://mysite.com/ajax/request to /ajax/request and Its working.

如果需要使用跨域,则需要使用jsonp作为数据类型.

If you need to use cross domains, you need to use jsonp as your dataType.

非常感谢evildead

Many thanks to evildead

我对服务器的JSON请求返回一个空响应. 仅在Windows Vista计算机上的Firefox 6.0.2和Safari中会发生这种情况.

My JSON request to my server returns an empty response. This only happens in Firefox 6.0.2 and Safari in a Windows Vista machine.

输出由php脚本生成,并具有json/application标头.

The output is generated by a php script and has json/application headers.

这将返回空响应:

    $('#ajaxcall').click(function(){
var ts = new Date().getTime();
var urlz = $('#targeturl').val()+'/'+ts;
var dataString = $("#datazz").val();
$.ajax({  
    type: "POST", url: urlz, data: "data="+dataString, 
    success: function(data){  
        var obj = jQuery.parseJSON(data);

        for (var i = 0; i < obj.length; i++) {
            var object = obj[i];
            for (property in object) {
            var s = property + "=" + object[property] + "<br>";
                $("#console").after(s);
            }
        }
    }  
});
});

还有这个:

$( "#tags" ).autocomplete({
        source: function( request, response ) {
            $.post("http://mysite.com/v2/ajax/tag_suggestion/ab", {data:request.term}, function(data){
                response($.map(data, function(item) {

                if ($('#tagsboxvals').hasClass(item.name.split(' ').join('_'))){
                return null;

                } else {
                return {
                    label: item.name,
                    value: item.name
                }
                }
                }))
            }, "json");
            },
            ....
});

感谢您的帮助

这是PHP脚本生成的内容

This is what the PHP script is generating:

$arr = array(
    array('name'=>'pizza', 'point'=>'1'),
    array('name'=>'blt', 'point'=>'1'));

    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    echo json_encode($arr);

这是格式正确的JSON文档.

It's a well formed JSON document.

标题: 响应标题 日期,2011年9月7日星期三23:58:42 GMT 服务器Apache/2.2.3(CentOS) X-Powered-by PHP/5.1.6 到期时间:格林尼治标准时间1997年7月26日,星期一05:00:00 缓存控制无缓存,必须重新验证 语法无缓存 内容长度29 连接关闭 内容类型应用程序/json 请求标题 托管mysite.com 用户代理Mozilla/5.0(Macintosh; Intel Mac OS X 10.7; rv:6.0)Gecko/20100101 Firefox/6.0 接受application/json,text/javascript,/; q = 0.01 接受语言en-us,en; q = 0.5 接受编码gzip,放气 接受字符集ISO-8859-1,utf-8; q = 0.7,*; q = 0.7 连接保持活动 内容类型应用程序/x-www-form-urlencoded;字符集= UTF-8 X-Requested-With XMLHttpRequest 引荐 http://mysite.com/v2/user/register 内容长度8 Cookie城市=纽约; __utma = 100174657.1435105779.1308773648.1314994226.1315368765.113; __utmz = 100174657.1315368765.113.98.utmcsr = mysite.com | utmccn =(referral)| utmcmd = referral | utmcct =

Headers: Response Headers Date Wed, 07 Sep 2011 23:58:42 GMT Server Apache/2.2.3 (CentOS) X-Powered-By PHP/5.1.6 Expires Mon, 26 Jul 1997 05:00:00 GMT Cache-Control no-cache, must-revalidate Pragma no-cache Content-Length 29 Connection close Content-Type application/json Request Headers Host mysite.com User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:6.0) Gecko/20100101 Firefox/6.0 Accept application/json, text/javascript, /; q=0.01 Accept-Language en-us,en;q=0.5 Accept-Encoding gzip, deflate Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection keep-alive Content-Type application/x-www-form-urlencoded; charset=UTF-8 X-Requested-With XMLHttpRequest Referer http://mysite.com/v2/user/register Content-Length 8 Cookie city=new york; __utma=100174657.1435105779.1308773648.1314994226.1315368765.113; __utmz=100174657.1315368765.113.98.utmcsr=mysite.com|utmccn=(referral)|utmcmd=referral|utmcct=

标头发布响应JSON 对象{name ="pizza",point = 1} [对象{name ="pizza",point = 1}]

Headers Post Response JSON Object { name="pizza", point=1} [Object { name="pizza", point=1}]

它可以在同一台计算机上与chrome一起正常使用,但不适用于Firefox和Safari.

It works fine with chrome on the same machine, but not firefox and safari.

推荐答案

您必须确保您的返回值是"really" json.有些浏览器不接受语法上不正确的json.

You have to make sure, that your return values are "really" json. Some browsers dont accept json which isn't syntactically correct.

因此请确保100%确保返回以下内容:

so make 100% sure you return something like:

{"foo": 1, "bar": "foobar"}

这是正确的json.

例如这不是:

{'foo': 1, 'bar': "foobar"}

这也是错误的:

{foo: 1, bar: "foobar"}

有关您的javascript代码的更多建议:

further some advices to your javascript code:

   return {
                label: item.name,
                value: item.name
            }

这是不正确的,请将密钥用引号引起来. 返回 { 标签":item.name, 值":item.name }

this is not correct, wrap your keys in quotes. return { "label": item.name, "value": item.name }

通常我已经读过很多次了,您应该将ajax调用作为GET Requests而不是POST,因为POST会产生更多的开销和流量.但是不要为此钉我.

Generally I've read many time you should do ajax calls as GET Requests not POST, because POST produces more overhead and traffic. But don't nail me for that.

此外,当您想返回例如php中的数组,请使用json_encode($ var)

Further, when you want to return e.g. an array from within php, use json_encode($var)

http://php.net/manual/de/function.json- encode.php

为您的具体用例返回:

{"name": "pizza", "point": 1}

在您的php脚本中.

在萤火虫的页面上尝试一下:

Try this on your page in firebug:

var obj = jQuery.parseJSON('[{"name":"pizza","point":1}]');

    for (var i = 0; i < obj.length; i++) {
        var object = obj[i];
        for (property in object) {
        var s = property + "=" + object[property] + "<br>";
            $("#console").after(s); console.log(s)
        }
    }

这篇关于JSON在FireFox&amp;上返回空响应Safari(Windows Vista)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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