使用Json和Jquery进行Parseerror

时间:2012-01-28 14:23:09

标签: jquery json parse-error

我有以下Json:

[{"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));
    }
}

3 个答案:

答案 0 :(得分:1)

确保您的服务器正在将标头响应设置为application/json。这有时会导致在响应中解析html,具体取决于您的后端。

答案 1 :(得分:1)

除了你从未对AJAX调用的结果做任何事情之外,你的代码看不出任何问题。这是一个full working demo。我怀疑你的服务器可能会以某种方式返回正确的JSON,这是导致错误的最可能原因。您应该确保服务器以状态代码200响应,将Content-Type标头设置为application/json并在响应正文中发送确切的JSON。在此AJAX请求期间,使用FireBug或类似工具分析通过线路发送的确切内容。

此外,您似乎还没有发送术语请求参数。试试这样:

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
});

答案 2 :(得分:0)

我没有使用Java,但是我尝试使用PHP解码您的JSON时遇到类似的解析错误。我不得不操纵你的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"}
    }
}

我试图让它解析而不必定义内部数字键但是我仍然得到一个解析错误。希望这会有所帮助。

相关问题