Javascript 带有jquery和ajax解析JSON的Problen。仅在firefox中工作
我有这个jquery代码:Javascript 带有jquery和ajax解析JSON的Problen。仅在firefox中工作,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,我有这个jquery代码: var baseURl = 'http://www.testdomain.com'; bindItemImage("230015"); function bindItemImage(_itemCode) { $.ajax({ url: baseURl + 'v3/api/itemimage/' + _itemCode, type: 'GET', contentType: "application/json;charset=utf-8",
var baseURl = 'http://www.testdomain.com';
bindItemImage("230015");
function bindItemImage(_itemCode) {
$.ajax({
url: baseURl + 'v3/api/itemimage/' + _itemCode,
type: 'GET',
contentType: "application/json;charset=utf-8",
success: function (data) {
var item = $.parseJSON(data);
var file = baseURl + item.File;
$('.itemPhoto').attr('src', file);
}
});
}
它在firefox中可以很好地显示图像,但在IE或chrome中却不能。
Chrome和IE返回正确的数据,但我收到一条JS错误消息“无法读取null的属性'File'”
这是JS小提琴
谢谢当您期待json响应时,请使用
contentType:“application/json;charset=utf-8”将数据类型设置为“json”
,
用于在请求中发送json。另外,当您将json指定为数据类型时,它在传递给成功处理程序时已经被解析
$.ajax({
url: baseURl + 'v3/api/itemimage/' + _itemCode,
type: 'GET',
dataType: "json",
success: function (data) {
var file = baseURl + data.File;
$('.itemPhoto').attr('src', file);
}
});
项在其他浏览器中显示为未定义项。首先尝试从url:
中删除baseURl
,然后添加数据类型:“json”
并删除$。parseJSON
是否尝试跨域调用?@vts如果这是跨域请求,则$。ajax
不支持Internet Explorer中的跨域请求,您必须使用