0

I make an ajax call with jquery $.get() which returns a json array and it works fine on some cases. On other cases however, in Firefox i get a weird error saying (translated from german):

XML-processing error: syntax error

This is the structure of the json as shown in my servlet int the java console:

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

And this is the structure when logged in ff console using JSON.stringify():

[{\"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:

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?

2
  • can you post your $.get() request please. Commented Jul 27, 2017 at 11:16
  • Have you tried UTF-8 encoding on both your code page and the response source? Commented Jul 27, 2017 at 11:17

1 Answer 1

1

Looks like that you'r response is not processed by jQuery as you expected. 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)

because as I can see from you'r post, looks like that you pass a string to JSON.stringify

I hope that i help you, have a nice day

Sign up to request clarification or add additional context in comments.

1 Comment

just had the same problem ... had to try the solution. "dataType" is not needed in the version of the pure enumeration. Just add $.get a new parameter "json". <pre> <code> $.get(url, {"operation": "search", "searchText": searchText, "types": types, "resultNumber": 99}, function(data, status){ console.log(JSON.stringify(data)); }, "json" ); </code></pre>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.