2010-09-17 74 views
0

我有一個jQuery函數從PHP頁面中檢索JSON響應。傳遞給getJSON()的參數之一是一年。如果我傳遞一個int(2008)Firebug報告一個404錯誤並且getJSON調用失敗,但是如果我傳遞一個字符串(x2008),Firebug會報告一個200代碼並觸發回調。在這兩種情況下,PHP頁面都會返回相同的響應 - 它會自動將x2008檢測爲無效,並使用2010的默認值 - 並且Firebug會顯示它已收到響應。如果我將int作爲字符串傳遞,它也會失敗('2008')。當參數爲int時getJSON失敗

這裏的的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); 
}); 

螢火報告正確的反應,即使它說有一個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並失敗。

+0

實際上,它看起來可能是服務器或瀏覽器問題。如果我直接加載PHP文件,我可以在Chrome中獲得200,但在Firefox和IE中獲得404。你可以在這裏看到: http://willingcook.com/wp-content/plugins/manage-recipes/controllers/ajax-controller.php?call=getWeekList&year=2007 - 404 http://willingcook.com/ wp-content/plugins/manage-recipes/controllers/ajax-controller.php?call = getWeekList&year = x2007 - 200 – 2010-09-17 22:45:39

回答

1

您是否嘗試過的狀態代碼設置爲當您正在構建的JSON的頭PHP頁面?

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

工作,謝謝:)我想知道爲什麼服務器不會自己返回正確的代碼,但。 – 2010-09-18 15:29:57

0

如果你正在返回JSON,內容類型不應該text/html的,它應該是application/json

+0

謝謝epascarello。我解決了這個問題,但它似乎並沒有影響到這個問題。 – 2010-09-17 18:10:45

相關問題