weixin_33733810 2014-01-22 14:40 采纳率: 0%
浏览 19

jQuery中的JSON解析错误

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?

  • 写回答

1条回答 默认 最新

  • 撒拉嘿哟木头 2014-01-22 14:43
    关注

    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;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R