使用JavaScript,AJAX和功放的问题; JSON [英] Problem with Javascript, AJAX & JSON

查看:85
本文介绍了使用JavaScript,AJAX和功放的问题; JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个iPhone开发的相当新的Web开发,所以请原谅我,如果我在这里做愚蠢的错误:

I'm an iPhone Developer whose quite new to web development so please forgive me if I'm making silly mistakes here:

当用户从下拉一个项目我想要做到的是,它会调用一些javascript,它衬托一个AJAX请求到PHP文件我有一个返回一些JSON。 - 这JSON包括缩略图文件名,标题为图像。这些都需要在我的文档分别传递到图像,文本框。

What I'm trying to accomplish is when the user selects an item from a drop-down, it calls some javascript, which sets off an AJAX request to a PHP file I have which returns some JSON. - That JSON includes a thumbnail image filename, and a caption for the image. These need to be passed in to an image, and textfield respectively in my document.

现在的问题是,它没有试图给eval的JSON后。 - 以同样的情况的eval(JSON); JSON.parse(JSON); 我工作了这一点,通过调用文件撰写('东西');定期在我的回应方式。 - 它随时都可以写上去的地方它解析JSON点

The problem is, it fails after trying to eval the json. - The same happens with eval(json); and JSON.parse(json); I worked this out by calling document.write('something'); periodically in my response method. - It will always be able to write up to the point where it parses the json.

我相信我的PHP是有效的,我的要求是很好的(我能输出从JS罚款响应)。

I am sure my PHP is valid and my request is good (I can output the response from the JS fine).

下面是我打电话的时候,一个新的项目,在下拉列表中选择的功能:

Here is the function I call when a new item is selected in the drop-down:

function changeImage(id){
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        var res=xmlhttp.responseText;
        var responseObject = eval(res);
        var caption = responseObject.caption;
        var textField = document.getElementById('caption');
        textField.value = caption;
        var imagePreview = document.getElementById('imgPreview');
        imagePreview.src = responseObject.filename;
        }
      }
    xmlhttp.open("GET","getCaption.php?id="+id,true);
    xmlhttp.send();
}

在我的输出RES(或手动访问PHP文件)的JSON看起来是这样的:

When I output res (or access the php file manually) the JSON looks like this:

{"caption":"A caption","filename":"myimage_thumb.jpg"}

为什么是JSON的eval失败?

why is the JSON eval failing?

更新: 记录此错误:语法错误:解析错误​​

推荐答案

您正在做一个JSON字符串和pretending,它是一个Javascript对象常量。但是,请记住, JSON仅仅是 JavaScript的对象符号!名为

You're taking a JSON string and pretending that it's a Javascript object literal. But, remember, JSON is merely named after Javascript's object notation!

你那么希望评估将直接评估这一点,并返回一个对象,但这种情况并非如此。

You're then hoping that eval will directly evaluate this and return an object, but this is not the case.

编写以下的Firebug控制台:

Writing the following in the Firebug console:

eval('{"caption":"A caption","filename":"myimage_thumb.jpg"}')

也将产生一个SyntaxError。 (理想情况下,你已经缩小你的问题到这个!)

will also yield a SyntaxError. (Ideally, you'd have narrowed your problem down to this!)

有趣的是,构建一个目标更明确地不工作(此输入):

Interestingly, constructing an object more explicitly does work (for this input):

eval('new Object({"caption":"A caption","filename":"myimage_thumb.jpg"})')

由于不只是迫使投入是某种EX pression,使用括号:

As does just forcing the input into being a certain kind of expression, with parentheses:

eval('({"caption":"A caption","filename":"myimage_thumb.jpg"})')


说明

这一切都在ECMAScript标准归结为这一说法从12.4:


Explanation

It all comes down to this statement from 12.4 in the ECMAScript standard:

请注意,防爆pressionStatement 不能用大括号开始,因为这可能会使它与暧昧块。

Note that an ExpressionStatement cannot start with an opening curly brace because that might make it ambiguous with a Block.

<一个href="http://rayfd.word$p$pss.com/2007/03/28/why-wont-eval-eval-my-json-or-json-object-object-literal/">This文章解释了问题的深度,但在总结的解决方案是使用 JSON.parse(RES)来获得一个JSON字符串对象。这是正确的做法,只要你输入有效的JSON也不会失败。

This article explains the issue in depth, but in summary the solution is to use JSON.parse(res) to obtain an object from a JSON string. This is the proper approach, and it will not fail as long as your input is valid JSON.

这篇关于使用JavaScript,AJAX和功放的问题; JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆