【问题标题】:Parseerror with Json and JqueryJson 和 Jquery 的解析错误
【发布时间】:2012-01-28 14:23:09
【问题描述】:

我有以下 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 中的解析错误。

我实际使用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));
    }
}

【问题讨论】:

    标签: jquery json parse-error


    【解决方案1】:

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

    此外,您似乎从未发送过 term 请求参数。试试这样:

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

    【讨论】:

    • 达林。非常感谢演示。我确实发现了使用 Chrome 的网络工具的问题。内容类型设置为“text/html”。这很奇怪,因为我确实指定了TextStreamResponse("application/json","UTF-8", gson.toJson(postcodes));
    • @balteo,回复的内容呢?它实际上是 JSON 吗?我怀疑服务器将其设置为 text/html 因为服务器上出现错误并呈现错误页面。您设置 dataType: 'json' 的事实意味着 jQuery 忽略服务器 Content-Type 标头并假定为 JSON。但是,如果服务器没有在响应中发送正确的 JSON,它将无法工作。所以修复你的服务器端代码。
    • 找到问题了!!这是由于错误指定的网址:url : 'utils/JSonPostcodesWithQueryParam', 我的基本错误!谢谢大家!!它实际上解析到我的应用程序的主页...
    【解决方案2】:

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

    【讨论】:

    • 你这个解析错误可能是由于方括号括起来的json数据吗?
    • 方括号只表示它是一个数组。这没有什么问题。
    【解决方案3】:

    我没有使用 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"}
        }
    }
    

    我试图在不必定义内部数字键的情况下对其进行解析,但是我仍然遇到解析错误。希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-08-28
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-13
      • 2017-07-31
      • 1970-01-01
      相关资源
      最近更新 更多