Hi I have a simple JSON output:

嗨,我有一个简单的JSON输出:

"{
   \"max_output_watts\": 150, 
   \"frame_length_inches\": \"62.20\",
   \"frame_width_inches\": \"31.81\"
 }"

which I use in a function like so:

我在这样的函数中使用它:

...
$.getJSON(pop_model_dims_url, {model_id: selected}, function(data, jqXHR){
              alert(data.max_output_watts);
          });
...

The alert is just a placeholder for now, but I don't understand why the alert value is 'undefined'. If I do:

警报现在只是一个占位符,但我不明白为什么警报值是'未定义'。如果我做:

...
$.getJSON(pop_model_dims_url, {model_id: selected}, function(data, jqXHR){
              alert(data);
          });
...

I get the full json dump as expected.

我按预期得到了完整的json转储。

Any help much appreciated.

任何帮助非常感谢。

4 个解决方案

#1


5

Since your alert(data); gives you the full JSON dump, this likely means that the JSON has been double encoded on the server.

自你的警报(数据);为您提供完整的JSON转储,这可能意味着JSON已在服务器上进行了双重编码。

jQuery will have parsed it once for you, but you'd need to parse it again.

jQuery会为你解析一次,但是你需要再次解析它。

$.getJSON(pop_model_dims_url, {model_id: selected}, function(data, jqXHR){
    data = $.parseJSON(data);
    alert(data.max_output_watts);
});

Of course this is not a proper fix. The proper solution is to fix it on the server.

当然这不是一个正确的解决方案。正确的解决方案是将其修复在服务器上。

Once you've done that, you won't need $.parseJSON anymore.

完成后,您将不再需要$ .parseJSON。

更多相关文章

  1. 我如何捕获并插入Meteor.Error警报从Meteor.Methods到客户端数据
  2. Javascript警报中文本的颜色
  3. 等待执行所有ajax回调的最佳解决方案
  4. 【问题解决方案】ImportError: No module named 'pygal'
  5. Python 黏包及黏包解决方案
  6. 无法安装ndg-httpsclient或者我的解决方案错误
  7. Linux无法连接网络解决方案
  8. Media-S 简介(一个开源的DRM解决方案)
  9. AppScan安全问题解决方案

随机推荐

  1. 占位的实现和间接实现从快捷建中启动一个
  2. Android应用程序结构介绍
  3. Android底部导航栏实现(一)之BottomNavigat
  4. 解决Android SDK Manager不能访问Google
  5. [Android]定制RadioButton样式
  6. 50个安卓开发者应该熟悉的Android Studio
  7. EditText的属性
  8. UI控件--EditText
  9. Android调用系统分享,资源未找到问题
  10. 安卓开发学习笔记