2

I am working on an in-house web app that uses Pylons on the backend, and I find myself in need of help figuring out why I'm getting JSON parsing errors.

The Python routine on the server is effectively this:

import json

# Other Pylons imports here

# Snip...

def validateMachine(self):
  retObj = {}
  retObj['ipv4addr'] = '10.10.15.9'
  retObj['netmask'] = '255.255.255.0'
  return json.dumps(retObj)

The client side has the following jQuery code:

$.ajax({
  type: "POST",
  url: "/kickstart/validateMachine",
  data: {theData: theValue},
  dataType: "json"
 })
 .done(function(data) {
  retObj = $.parseJSON(data);
  #Other code here
 });

When I execute the AJAX query, the server routine returns correctly, but the call to $.parseJSON() errors out. A screenshot of the Firebug console after it's errored out: JSON error console

The response appears to be valid strict JSON, so my question is two-fold: why is it not parsing correctly, and how can I get it to do so? It's my understanding that jQuery is (correctly) attempting to use the browser's native JSON parser in this case - can I somehow override that and tell jQuery to not use the native parser?

2
  • IMHO, when setting the expected dataType to JSON, you don't need to parse manually, data is JSON already. Have you checked typeof data? Commented Jan 22, 2014 at 14:44
  • When you specify json, jQuery automatically parses the response for you. Therefore, data is already a native js object, and calling parseJSON on the js object causes an error. Commented Jan 22, 2014 at 14:45

1 Answer 1

2

You cannot parseJSON on an object that is already a json object, seeing that the data is already a JSON according to your images

{"netmask": "255.255.255.0", "ipv4addr": "10.10.15.9"}

So this should be enough

retObj = data;
Sign up to request clarification or add additional context in comments.

Comments

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.