简体   繁体   English

使用Json和Jquery进行Parseerror

[英]Parseerror with Json and Jquery

I have the following Json: 我有以下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"}]

It causes a parseerror in JQuery. 它导致JQuery中的parseerror。

I actually use the jquery.ui autocomplete control as follows: 我实际上使用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
            });
        });

I am not sure what I am getting wrong as my Json seems correct... 因为我的Json看似正确,我不确定我错了什么...

Any one has any clue? 任何人都有任何线索?

EDIT: 编辑:

Here is what generates the 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));
    }
}

Make sure your server is setting the header response to application/json . 确保您的服务器正在设置对application/json的标头响应。 This can sometimes cause html to be parsed in the response depending on your backend. 这有时会导致在响应中解析html,具体取决于您的后端。

Can't see anything wrong with your code other the fact that you never do anything with the results of your AJAX call. 除了您的AJAX调用结果之外,您从未对代码有任何不妥之处。 Here's a full working demo . 这是一个完整的工作演示 I suspect that your server might somehow not return correct JSON which is the most likely cause of the error. 我怀疑你的服务器可能会以某种方式返回正确的JSON,这是导致错误的最可能原因。 You should make absolutely sure that the server responds with status code 200, sets Content-Type header to application/json and sends the exact JSON in the response body. 您应该确保服务器以状态代码200响应,将Content-Type标头设置为application/json并在响应正文中发送确切的JSON。 Use FireBug or similar tool to analyse what exactly is sent over the wire during this AJAX request. 在此AJAX请求期间,使用FireBug或类似工具分析通过线路发送的确切内容。

Also you don't seem to ever be sending the term request parameter. 您似乎也没有发送术语请求参数。 Try like this: 试试这样:

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

I'm not using Java but I was getting a similar parse error attempting to decode your JSON with PHP. 我没有使用Java但是我在尝试用PHP解码你的JSON时遇到了类似的解析错误。 I had to manipulate your JSON to this to parse it properly: 我不得不操纵你的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"}
    }
}

I attempted to get it to parse without having to define the inner, numerical keys however I was still getting a parse error. 我试图让它解析,而不必定义内部,数字键,但我仍然得到一个解析错误。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM