IE 中的 jquery .ajax(或 .eval)问题
希望有人能为我阐明这个问题。
我有一个小应用程序(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用
eval
,它是邪恶的。另请确保您的服务器发送正确的内容类型 (application/json):此外,您可能不需要使用 POST 请求
cache: false
。这些可能是您从 IE 和 GET 请求得到的反应:-)Don't use
eval
, it's evil. Also make sure your server sends proper content type (application/json):Also you probably don't need
cache: false
with a POST request. Those are probably reflexes that you got from IE and GET requests :-)这一行:
应该是:
Javascript 标识符区分大小写。
This line:
should be:
Javascript identifiers are case sensitive.