IE 中的 jquery .ajax(或 .eval)问题

发布于 2024-10-17 08:43:33 字数 1127 浏览 1 评论 0原文

希望有人能为我阐明这个问题。

我有一个小应用程序(http://www.bentinckfencing.co.uk/fence_calculator.php)

我有一个php文件,它发送回JSON编码的字符串。

我使用 jQuery 通过 .ajax() 函数检索此字符串。获得 JSON 字符串后,我将以这种方式使用 eval()

$.ajax({
   type: "POST",
   url: "ajax-test.php",
   cache: false,
   data: "choice=34",
   success: function(data){

       var myObject = eval('(' + data + ')');

       //Now I set lots of variables in this manner and use them later in the script

       var productName  = myObject[0]['name'];
       var productPrice = myObject[0]['price'];                                     
       var productID    = myObject[0]['id'];

        }
   });

一如既往,这在所有浏览器中都运行良好,除了 IE 抛出错误 object does not support this property或方法

我只是想不出我哪里出错了。如果有人有时间提供帮助,甚至检查我的应用程序,我将是最有帮助的:)

编辑 我发现 Ajax 调用成功检索 IE 中的数据。它的字符串像这样 [{"name":" 12 英寸岩面砾石板","ID":"106","price":"7.00"},{"name":" 12 英寸双面砾石Board","ID":"108","price":"10.50"},{"name":"12英寸光滑砾石板","ID":"109","price":"7.00"}]

所以现在我的问题是如何处理 JS 拥有的数据......任何想法都会很棒:)

Hopefully someone can shed some light onto this issue for me.

I have a little APP (http://www.bentinckfencing.co.uk/fence_calculator.php)

I have a php file that sends a JSON encoded string back.

Im using jQuery to retrieve this string with the .ajax() function. After I have the JSON string, I am then using eval() in this manner

$.ajax({
   type: "POST",
   url: "ajax-test.php",
   cache: false,
   data: "choice=34",
   success: function(data){

       var myObject = eval('(' + data + ')');

       //Now I set lots of variables in this manner and use them later in the script

       var productName  = myObject[0]['name'];
       var productPrice = myObject[0]['price'];                                     
       var productID    = myObject[0]['id'];

        }
   });

As always, this works well in all browsers except IE which is throwing the error object doesn't support this property or method

I just can't think where I'm going wrong.. If anyone has time to help, or even to check out my app, I'm be most helpful :)

edit
I've found that the Ajax call is successfully retrieving the data in IE. It's string like this [{"name":" 12 inch Rock Face Gravel Board","ID":"106","price":"7.00"},{"name":" 12 inch Double Sided Gravel Board","ID":"108","price":"10.50"},{"name":" 12 inch Smooth Gravel Boards","ID":"109","price":"7.00"}]

So now my issue is down to how I handle the data now that JS has it... Any ideas would be great :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花伊自在美 2024-10-24 08:43:33

不要使用eval,它是邪恶的。另请确保您的服务器发送正确的内容类型 (application/json):

$.ajax({
    type: 'POST',
    url: 'ajax-test.php',
    cache: false,
    data: { choice: 34 },
    dataType: 'json',
    success: function(items) {
        // TODO: some range checking on this array won't be useless
        // before trying to access its elements

        var productName = items[0].name;
        var productPrice = item[0].price;
        var productID = items[0].ID;

        // TODO: do something useful with those variables
    }
});

此外,您可能不需要使用 POST 请求 cache: false 。这些可能是您从 IE 和 GET 请求得到的反应:-)

Don't use eval, it's evil. Also make sure your server sends proper content type (application/json):

$.ajax({
    type: 'POST',
    url: 'ajax-test.php',
    cache: false,
    data: { choice: 34 },
    dataType: 'json',
    success: function(items) {
        // TODO: some range checking on this array won't be useless
        // before trying to access its elements

        var productName = items[0].name;
        var productPrice = item[0].price;
        var productID = items[0].ID;

        // TODO: do something useful with those variables
    }
});

Also you probably don't need cache: false with a POST request. Those are probably reflexes that you got from IE and GET requests :-)

笑着哭最痛 2024-10-24 08:43:33

这一行:

   var productID    = myObject[0]['id'];

应该是:

   var productID    = myObject[0]['ID'];

Javascript 标识符区分大小写。

This line:

   var productID    = myObject[0]['id'];

should be:

   var productID    = myObject[0]['ID'];

Javascript identifiers are case sensitive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文