jQuery ajax调用不起作用?! Firefox或XSS问题? [英] jquery ajax call not working?! firefox or xss problem?

查看:128
本文介绍了jQuery ajax调用不起作用?! Firefox或XSS问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:在Firefox中,我没有任何回应.在即它工作正常. 我想对本地脚本进行ajax调用,以纯文本或其他形式获取一些信息.但这是行不通的.我认为跨脚本编写现在应该不是问题吗?

My problem is: in firefox i got no response. in ie it worked fine. I want a ajax call to a local script getting some information in plain text or something else. but it won't work. I think cross scripting should not a problem at this point or?

JavaScript代码很简单:

the javascript code is simple:

var targetUrl = "http://localhost/jQueryProxy.php";
var parameters = ""; // later

$.ajax({    
  type: "GET",
  async: true,
  url: targetUrl,
  data: parameters,
  success: function(param1, param2){
    alert(param1);
  }
});

以及php代码:

<?php
   header('Content-type: text/xml'));
   echo "test";
?>

推荐答案

尝试var targetUrl = "/jQueryProxy.php";
另外,您可以检查Firefox的JavaScript控制台以查找错误: Ctrl + Shift + J

try var targetUrl = "/jQueryProxy.php";
Also, you can check Firefox's javascript console to look for an error: Ctrl+Shift+J

您还可以尝试通过添加处理程序

You can also try looking for jQuery's Ajax error message, by adding an handler (source):

error:function (xhr, ajaxOptions, thrownError){
    alert(xhr.status);
    alert(xhr.statusText);
    alert(thrownError);
 }  

更新:我已经进行了一些测试,似乎Firefox阻止了Ajax从本地文件到Web的访问(对于本地主机,也是如此),但没有引发异常.在success处使用alert($('*', param1).text());会显示当前文档的内容,这很奇怪.
将XML放置为本地文件也不起作用,FF和IE的行为不一致-它们的行为非常不同.
最好的选择是将html放在服务器(本地主机)上,与xml文件位于同一端口(此处为80).
另外,当您的xml有效时,请考虑添加dataType:'xml'.

Update: I've done some testing, is seems Firefox blocks Ajax from local files to the web (localhost too, for that matter), but does not throw an exception. Using alert($('*', param1).text()); at success shows the content of the current document, which is weird.
Placing the XML as a local file doesn't work either, the behavior of FF and IE is inconsistency - they act very differently.
Your best bet is to place the html on the server (localhost), on the same port as your xml file (80 here).
Also, when your xml is valid, consider adding dataType:'xml'.

这篇关于jQuery ajax调用不起作用?! Firefox或XSS问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆