
jquery ajax 跨域请求问题
第一个:http://127.0.0.1/json.txt第二个:http://127.0.0.1:84/json.txt这两地址通过浏览器访问都正常,但是我在第一个里通...
第一个:http://127.0.0.1/json.txt第二个:http://127.0.0.1:84/json.txt这两地址通过浏览器访问都正常,但是我在第一个里通过ajax请求第二个也就是端口为84的json.txt文件要怎么返回值呢
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="script/jquery-1.7.1.min.js" type="text/javascript"></script>
<script>
$(function (){
$("#send").click(function (){
alert("同域请求");
$.ajax({
type : "get",
async:true,
url:"http://127.0.0.1/json.txt",
dataType : "json",
beforeSend: function(xhr) { //beforeSend定义全局变量
xhr.setRequestHeader("If-Modified-Since", "0"); //If-Modified-Since HTTP请求头标签,即比较浏览器缓存页面时间
},
success : function(xhr){
alert(xhr.a);
},
error:function(){
alert('错误');
}
});
})
})
$(function (){
$("#send2").click(function (){
alert("跨域请求")
$.ajax({
type : "get",
async:true,
url:"http://127.0.0.1:84/json.txt",
dataType : "json",
beforeSend: function(xhr) { //beforeSend定义全局变量
xhr.setRequestHeader("If-Modified-Since", "0"); //If-Modified-Since HTTP请求头标签,即比较浏览器缓存页面时间
},
success : function(xhr){
alert("服务器返回值为:"+xhr);
alert(xhr.a);
},
error:function(){
alert('错误');
}
});
})
})
</script>
</head>
<body>
<br /><br />
<input type="button" id="send" value="同域请求能正常得到值" />
<br /><br />
<input type="button" id="send2" value="跨域请求" />
</body>
</html> 展开
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="script/jquery-1.7.1.min.js" type="text/javascript"></script>
<script>
$(function (){
$("#send").click(function (){
alert("同域请求");
$.ajax({
type : "get",
async:true,
url:"http://127.0.0.1/json.txt",
dataType : "json",
beforeSend: function(xhr) { //beforeSend定义全局变量
xhr.setRequestHeader("If-Modified-Since", "0"); //If-Modified-Since HTTP请求头标签,即比较浏览器缓存页面时间
},
success : function(xhr){
alert(xhr.a);
},
error:function(){
alert('错误');
}
});
})
})
$(function (){
$("#send2").click(function (){
alert("跨域请求")
$.ajax({
type : "get",
async:true,
url:"http://127.0.0.1:84/json.txt",
dataType : "json",
beforeSend: function(xhr) { //beforeSend定义全局变量
xhr.setRequestHeader("If-Modified-Since", "0"); //If-Modified-Since HTTP请求头标签,即比较浏览器缓存页面时间
},
success : function(xhr){
alert("服务器返回值为:"+xhr);
alert(xhr.a);
},
error:function(){
alert('错误');
}
});
})
})
</script>
</head>
<body>
<br /><br />
<input type="button" id="send" value="同域请求能正常得到值" />
<br /><br />
<input type="button" id="send2" value="跨域请求" />
</body>
</html> 展开
3个回答
展开全部
ajax不能跨域的 你这个请求不可能能成功
如果 ajax请求无返回 例如 只是上报数据这种可以参考 这些 做网站流量统计的方式 伪装一个 1像素的图片 然后 通过url将参数传递过去
如果有返回 而且必须要使用 建议使用 ajax请求自己的后端程序 让后端 去帮你抓取东西 然后返还到前台
如果 ajax请求无返回 例如 只是上报数据这种可以参考 这些 做网站流量统计的方式 伪装一个 1像素的图片 然后 通过url将参数传递过去
如果有返回 而且必须要使用 建议使用 ajax请求自己的后端程序 让后端 去帮你抓取东西 然后返还到前台
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个用JSONP,既然你用的是jQuery那就去看下文档里的 jQuery.getJSON 说明;
现在太晚了,就不写DEMO了吧。
现在太晚了,就不写DEMO了吧。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询