当参数为int时,getJSON失败

时间:2010-09-17 00:47:09

标签: jquery

我有一个jQuery函数,可以从PHP页面检索JSON响应。传递给getJSON()的参数之一是一年。如果我传递int(2008)Firebug报告404错误并且getJSON调用失败,但是如果我传递一个字符串(x2008),Firebug报告200代码并触发回调。在这两种情况下,PHP页面都返回相同的响应 - 它自动将x2008检测为无效并使用默认值2010 - 而Firebug显示它收到了响应。如果我将int作为字符串传递('2008'),它也会失败。

这是getJSON调用:


$mr.getJSON(controller, {call: 'getWeekList', year: '2008'}, function(data)
{
    var newList = '';           
    $mr.each(data, function(index, value)
    {
        newList += '' + value + '';
    } );
    newList += '';

    $mr("#selectWeekList").html(newList);
} );

Firebug报告了正确的回复,即使它说有404:


{"1":"December 30th - January 5th","2":"January 6th - January 12th","3":"January 13th - January 19th","4":"January 20th - January 26th","5":"January 27th - February 2nd","6":"February 3rd - February 9th","7":"February 10th - February 16th","8":"February 17th - February 23rd","9":"February 24th - March 1st","10":"March 2nd - March 8th","11":"March 9th - March 15th","12":"March 16th - March 22nd","13":"March 23rd - March 29th","14":"March 30th - April 5th","15":"April 6th - April 12th","16":"April 13th - April 19th","17":"April 20th - April 26th","18":"April 27th - May 3rd","19":"May 4th - May 10th","20":"May 11th - May 17th","21":"May 18th - May 24th","22":"May 25th - May 31st","23":"June 1st - June 7th","24":"June 8th - June 14th","25":"June 15th - June 21st","26":"June 22nd - June 28th","27":"June 29th - July 5th","28":"July 6th - July 12th","29":"July 13th - July 19th","30":"July 20th - July 26th","31":"July 27th - August 2nd","32":"August 3rd - August 9th","33":"August 10th - August 16th","34":"August 17th - August 23rd","35":"August 24th - August 30th","36":"August 31st - September 6th","37":"September 7th - September 13th","38":"September 14th - September 20th","39":"September 21st - September 27th","40":"September 28th - October 4th","41":"October 5th - October 11th","42":"October 12th - October 18th","43":"October 19th - October 25th","44":"October 26th - November 1st","45":"November 2nd - November 8th","46":"November 9th - November 15th","47":"November 16th - November 22nd","48":"November 23rd - November 29th","49":"November 30th - December 6th","50":"December 7th - December 13th","51":"December 14th - December 20th","52":"December 21st - December 27th"}

以下是Firebug报告的标题:


Response Headers
Date    Fri, 17 Sep 2010 00:07:41 GMT
Server  Apache/2.0.52 (CentOS)
X-Powered-By    PHP/5.1.6
Expires Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control   no-cache, must-revalidate, max-age=0
Pragma  no-cache
Set-Cookie  [redacted]
X-Pingback  [redacted]
Last-Modified   Fri, 17 Sep 2010 00:07:42 GMT
Content-Length  1690
Content-Type    text/html; charset=UTF-8
X-Cache MISS from [redacted], MISS from [redacted]
X-Cache-Lookup  MISS from [redacted], MISS from [redacted]
Via 1.0 [redacted] (squid/2.6.STABLE22), 1.0 [redacted] (squid/2.6.STABLE22)
Connection  close

Request Headers
Host    [redacted]
User-Agent  Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9
Accept  application/json, text/javascript, */*
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
Keep-Alive  115
Connection  keep-alive
Content-Type    application/x-www-form-urlencoded
X-Requested-With    XMLHttpRequest
Referer [redacted]
Cookie  [redacted]

响应似乎完全相同,当它没有时,显然它实际上不是404,因为它得到了响应,所以我无法弄清楚为什么Firebug认为有404并且失败。

2 个答案:

答案 0 :(得分:1)

在php页面中构建JSON时,您是否尝试将状态代码设置为标题?

header($_SERVER["SERVER_PROTOCOL"]." 200 OK");

答案 1 :(得分:0)

如果您要返回JSON,Content-Type不应该是 text / html ,它应该是application/json