本文核心部分参考了http://www.douban.com/group/topic/11504938/文章,类似于对原文的重排。
昨天调试代码,代码明明正确,使用FireBug返回的也是200,但是却提示“XML解析错误:未找到元素 位置:moz-nullprincipal”错误,搜了一些资料,但网上说的“异步转为同步”、“response为空”等原因都不能解决我的问题。
今天偶然路过豆瓣,发现有人也有相同的问题,原来是JavaScript跨域导致的。解决方法主要就那么几种,网上写得很详细,这里就只简略提一下,具体请大家自己搜索:要么把文件放到同一域下;要么使用servlet模拟请求ajax,本地ajax请求这个servlet完成调用;或者使用JSONP。
我自己用Jquery 的$.get()调用图书信息API 代码如下:
$('#send').click(function() {
$.get('http://api.douban.co
function getbook(data,status){
var newHTML;
if(status=='success')
{
$('.register-info').prepend(data);
} else {
$('.register-info').prepend('<p>错误</p>');
}
}
return false;
});
通过FIREbug 看到 返回的状态码是200 OK 但是出现如下错误
XML解析错误:未找到元素 位置:moz-nullprincipal:{9c1a51eb-25a1-4356-a6c8-878375d5c9ae} 行:1,列:1:
通过 alert(data.length); 显示 为 0 应该是根本就没有取到数据。
通过firebug 报错的 是JQUERY.JS 的5252行 内容如下
try {
5252 xhr.send( type === "POST" || type === "PUT" || type === "DELETE" ? s.data : null );
5253 } catch(e) {
5254 jQuery.handleError(s, xhr, null, e);
5255 // Fire the complete handlers
5256 complete();
5257 }
响应头信息
Server nginx
Date Fri, 21 May 2010 12:28:44 GMT
Content-Type text/xml; charset=utf-8
Connection keep-alive
Keep-Alive timeout=20
Content-Encoding gzip
Expires Sun, 1 Jan 2006 01:00:00 GMT
Pragma no-cache
Cache-Control must-revalidate, no-cache, private
WWW-Authenticate OAuth realm="http://www.douban.co
Set-Cookie bid="dKgZyZ5uTr0"; path=/; domain=.douban.com; expires=Thu, 01-Jan-2012 00:00:00 GMT
Content-Length 1411
联系客服