简体   繁体   English

Javascript,Ajax和JSON:解析响应时出现奇怪的“ xml处理”错误

[英]Javascript, Ajax & JSON: weird “xml processing” error when parsing response

I make an ajax call with jquery $.get() which returns a json array and it works fine on some cases. 我用jquery $.get()进行了ajax调用,它返回一个json数组,并且在某些情况下可以正常工作。 On other cases however, in Firefox i get a weird error saying (translated from german): 但是,在其他情况下,在Firefox中,我收到一个奇怪的错误消息(德语翻译):

XML-processing error: syntax error XML处理错误:语法错误

This is the structure of the json as shown in my servlet int the java console: 这是json的结构,如Java控制台中的servlet所示:

[{"key":"...","type":"...","content":"..."},
 {"key":"...","type":"...","content":"..."},
 ...]

And this is the structure when logged in ff console using JSON.stringify() : 这是使用JSON.stringify()登录ff控制台时的结构:

[{\"key":\"...",\"type":\"...",\"content":\"..."},
     {\"key":\"...",\"type":\"...",\"content":\"..."},
     ...]

My request simply looks like this: 我的请求看起来像这样:

$.get(url, 
        {"operation": "search", "searchText": searchText, "types": types, "resultNumber": 99},
        function(data, status){
            console.log(JSON.stringify(data));
    });

My java method for creating the JSON array: 我的用于创建JSON数组的java方法:

private JSONArray parseJSON (ArrayList<ResultObject> aResultList) throws JSONException
    {
        JSONArray resultJSONArray = new JSONArray();

        for (ResultObject resultObject : aResultList)
        {
            JSONObject jsonObject = new JSONObject();
            HashMap<String,String> fields = resultObject.getFields();

            for (Map.Entry<String, String> entry : fields.entrySet())
            {
                jsonObject.put(entry.getKey(), entry.getValue());
            }
            resultJSONArray.put(jsonObject);
        }
        return resultJSONArray;
    }

Whats going on here? 这里发生了什么? Where are all these backslashes in the returned json coming from? 返回的json中所有这些反斜杠来自何处?

Looks like that you'r response is not processed by jQuery as you expected. 看起来您的响应未如预期的那样由jQuery处理。 try to add a 尝试添加一个

dataType:"json"

as option for you'r request, also take care that you'r server-side set's the right header on HTTP response (Content-Type: application/json) 作为您请求的选项,还请注意服务器端在HTTP响应上设置了正确的标头(内容类型:application / json)

because as I can see from you'r post, looks like that you pass a string to JSON.stringify 因为从您的帖子中可以看到,就像您将字符串传递给JSON.stringify

I hope that i help you, have a nice day 希望我能对您有所帮助

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

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