2012-01-28 74 views
0

我有以下JSON:Parseerror使用JSON和jQuery

[{"label":"75001","value":"75001"}, 
{"label":"75002","value":"75002"}, 
{"label":"75003","value":"75003"}, 
{"label":"75004","value":"75004"}, 
{"label":"75005","value":"75005"}, 
{"label":"75006","value":"75006"}, 
{"label":"75007","value":"75007"}, 
{"label":"75008","value":"75008"}, 
{"label":"75009","value":"75009"}] 

它會導致JQuery的一個parseerror。

我實際使用的jquery.ui自動完成控制如下:

jQuery(document).ready(function() { 
      jQuery("#accountPostcode").autocomplete({ 
       source : function(request, response) { 
        var jqxhr = jQuery.ajax({ 
         url : "utils/JSonPostcodesWithQueryParam", 
         dataType : "json" 
        }).fail(function() { 
         console.log("error:"); 
         console.log(jqxhr.statusText); 
        }); 
       }, 
       minLength : 2 
      }); 
     }); 

我不知道我收到錯了,因爲我的Json似乎是正確的......

任何人有任何線索?

編輯:

這裏是產生JSON:

package com.bignibou.web.pages.utils; 

import java.util.List; 

import org.apache.tapestry5.EventConstants; 
import org.apache.tapestry5.StreamResponse; 
import org.apache.tapestry5.annotations.OnEvent; 
import org.apache.tapestry5.annotations.RequestParameter; 
import org.apache.tapestry5.ioc.annotations.Inject; 
import org.apache.tapestry5.util.TextStreamResponse; 

import com.bignibou.domain.utils.PostcodeJson; 
import com.bignibou.service.AccountService; 
import com.google.gson.Gson; 

public class JSonPostcodesWithQueryParam { 

    @Inject 
    private AccountService service; 

    @OnEvent(EventConstants.ACTIVATE) 
    StreamResponse loadPostcodes(@RequestParameter(value = "term") String beginningOfPostcode) { 
     Gson gson = new Gson(); 
     List<PostcodeJson> postcodes = service.loadPostcodes(beginningOfPostcode); 
     return new TextStreamResponse("application/json","UTF-8", gson.toJson(postcodes)); 
    } 
} 

回答

1

看不出你的代碼有什麼問題,其他的事實是你從來沒有對你的AJAX調用的結果做任何事情。這是full working demo。我懷疑你的服務器可能以某種方式不返回正確的JSON,這是最可能的錯誤原因。您應該確保服務器響應狀態代碼200,將Content-Type標頭設置爲application/json,並在響應正文中發送確切的JSON。使用FireBug或類似工具分析在此AJAX請求期間通過線路發送了什麼。

此外,你似乎永遠不會發送期限請求參數。嘗試是這樣的:

jQuery('#accountPostcode').autocomplete({ 
    source : function(request, response) { 
     var jqxhr = jQuery.ajax({ 
      url : 'utils/JSonPostcodesWithQueryParam', 
      data: { term: request.term }, 
      dataType : 'json' 
     }).fail(function() { 
      console.log('error:'); 
      console.log(jqxhr.statusText); 
     }).success(function(data) { 
      response(data); 
     }); 
    }, 
    minLength : 2 
}); 
+0

Darin。非常感謝演示。我使用Chrome的網絡工具發現問題。內容類型設置爲「text/html」。這很奇怪,因爲我確實指定了'TextStreamResponse(「application/json」,「UTF-8」,gson.toJson(postcodes)); ' – balteo 2012-01-28 14:57:36

+0

@balteo,迴應內容如何?它實際上是JSON嗎?我懷疑服務器將它設置爲text/html,因爲服務器出現錯誤,並且它顯示了一個錯誤頁面。你設置'dataType:'json''這一事實意味着jQuery忽略了服務器的Content-Type頭並假定了JSON。但是如果服務器在響應中沒有發送正確的JSON,它將無法工作。所以修復你的服務器端代碼。 – 2012-01-28 14:59:17

+0

發現問題!這是由於一個錯誤指定的url:'url:'utils/JSonPostcodesWithQueryParam','我的部分基本錯誤!謝謝!它實際上解決了我的應用程序的主頁... – balteo 2012-01-28 15:01:12

1

確保你的服務器設置頁眉響應application/json。這有時會導致html在響應中被解析,具體取決於您的後端。

+0

謝謝。查看我的應用程序代碼。 – balteo 2012-01-28 14:32:25

+0

你這個parseerror可能是由於括號內的json數據的方括號嗎? – balteo 2012-01-28 14:35:26

+1

方括號只表示它的一個數組。這沒什麼錯。 – Jashwant 2012-01-28 14:58:44

0

我沒有使用Java,但我得到了一個類似的解析錯誤試圖解碼您的JSON與PHP。我有你的JSON操縱這個正確分析它:

{ 
    "key": { 
     "0": {"label": "75001", "value": "75001"}, 
     "1": {"label": "75002", "value": "75002"}, 
     "2": {"label": "75003", "value": "75003"}, 
     "3": {"label": "75004", "value": "75004"}, 
     "4": {"label": "75005", "value": "75005"}, 
     "5": {"label": "75006", "value": "75006"}, 
     "6": {"label": "75007", "value": "75007"}, 
     "7": {"label": "75008", "value": "75008"}, 
     "8": {"label": "75009", "value": "75009"} 
    } 
} 

我試圖得到它,而不必限定內,數字鍵,但是我仍然得到解析錯誤解析。希望這可以幫助。