$.ajax 或 $.get 函数 get 仅在本地文件上接收
我需要使用 $.ajax 或 $.get 从 xml 文件获取获取响应。 问题是 $.ajax 或 $.get 函数似乎没有从远程服务器中的文件获得响应。它显然仅适用于本地文件。我正在从本地服务器开发网站,所以我的服务器主机类似于 127.0.0.1/。有人知道这个问题吗?或者我是否可以调试这个问题?我想我读过浏览器不允许跨站点 ajax 调用的地方..但是..那么我如何进行远程主机调用?
<script>
$(document).ready(function(){
$.ajax({
url: "<?=$request_url?>",
data: "<?=$data?>",
success: function(msg){
alert( "Data Saved: " + msg );
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$("#output").append(XMLHttpRequest.responseText + "<br />TextStatus: " + textStatus + "<br />ErrorThrown: " + errorThrown);
}
})
});
</script>
I need to use $.ajax or $.get to get fetch response from xml file.
The thing is that $.ajax or $.get function does not seem to get a response from files that are in remote server. It apparently works on local files only. I am developing the websites from local server, so my serverhost is like 127.0.0.1/. Does anyone know about this problem? Or is there anyway that I can debug this problem at all? I think I have read somewhere where browsers do not allow cross-site-ajax call..but..then how can I make remote host call?
<script>
$(document).ready(function(){
$.ajax({
url: "<?=$request_url?>",
data: "<?=$data?>",
success: function(msg){
alert( "Data Saved: " + msg );
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$("#output").append(XMLHttpRequest.responseText + "<br />TextStatus: " + textStatus + "<br />ErrorThrown: " + errorThrown);
}
})
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
幸运的是,由于跨域策略,您不能这样做。不过,您可以通过
$.ajax
调用来调用服务器端脚本,该调用又调用远程站点脚本。Fortunately, you cannot due to the cross-domain policy. You can however invoke a server side script via your
$.ajax
call which in turn calls on the remote site script.看看如何使用 JSONP
什么是 JSONP?
使用 jQuery 调用跨域 JSONP 分步指南
Have a look at using JSONP
What is JSONP?
Cross-domain JSONP with jQuery call step-by-step guide
是的,这是正确的,因为您无法使用这些方法访问另一个域上的内容。这是一个跨域脚本安全问题。
看一下与 jQuery 和 Javascript 相关的 XSS。
Yes, this is correct because you can't access content on another domain using those methods. This is a cross domain scripting security issue.
Take a look at XSS in relation to jQuery and Javascript.
由于安全限制,浏览器不允许跨域 ajax 调用,
但是仍然有一些解决方法。
尝试这些
http://james.padolsey.com/javascript/cross-域请求与jquery/
http:// /usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide
Browsers don't allow cross-domain ajax calls due to security restrictions
But there are still some workarounds in the wild.
Try These
http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide