【问题标题】:jQuery ajax does not work in IEjQuery ajax 在 IE 中不起作用
【发布时间】:2012-02-17 22:04:40
【问题描述】:

我在 WebLogic 10.3.2 上运行的 Web 应用程序中使用 jQuery 1.7.1。我对服务器执行 ajax GET。这一切在 FF 和 Chrome 中都可以正常工作,但 IE 8 中的 ajax 事件没有任何反应。就好像准备好的文档根本没有设置它们一样。

这是一些js:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <link rel="stylesheet" type="text/css" href="css/messaging.css" />
  <link rel="stylesheet" type="text/css" href="css/jquery-ui-1.8.17.custom.css" />
  <link rel="stylesheet" type="text/css" href="css/ui.dynatree.css" />
  <link rel="stylesheet" type="text/css" href="css/jquery.cluetip.css" />
  <link rel="stylesheet" type="text/css" href="css/jquery-ui-timepicker-addon.css" />
  <script type="text/javascript" src="js/jquery-1.7.1.js"></script>
  <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script>
  <script type="text/javascript" src="js/jquery.dynatree.js"></script>
  <script type="text/javascript" src="js/jquery.cookies.2.2.0.js"></script>
  <script type="text/javascript" src="js/jquery.cluetip.js"></script>
  <script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>
  <script type="text/javascript" src="js/jquery-ui-sliderAccess.js"></script>
  <script type="text/javascript">    
$(document).ready(function() {                                              
    $.ajaxSetup ({
        cache: false,
        xhrFields: {
            withCredentials: true
         },
         crossDomain: true
    });
...
$('#findSites').click(function() {                                // Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event...
searchVal = document.getElementById("searchFor").value;
searchTyp = document.getElementById("searchType").value;
$.get('SiteSearchServlet', {searchFor: searchVal, searchType: searchTyp}, function(responseJson) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response JSON...
...

我尝试过在 ajaxsetup 中仅使用 cache=false 而不使用字符集 - 没有帮助。单击 findSites 按钮时不会执行 $.get ajax 调用。

谁能告诉我让 jQuery ajax GET 在 IE 上工作的秘诀?

【问题讨论】:

  • 如果你将console.log()alert() 放在$.get() 所在的位置,that 会执行吗?

标签: jquery ajax internet-explorer


【解决方案1】:

尝试使用 $.ajax 而不是 $.get,这样您可以添加错误回调并查看 ajax 调用失败的原因。

http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/ (见最后...部分)

【讨论】:

  • 谢谢 - 我将“.error(function(jqXHR, textStatus, errorThrown) { alert("Error: " + errorThrown); })" 添加到我的 $.get 中(实际上是一样的)和出现“无运输”错误。谷歌搜索说这与跨域问题有关。然后我添加了这一行来强制 IE 允许这样做:“jQuery.support.cors = true; // 强制跨站点脚本”。这让 ajax 调用起作用了!希望对其他人有所帮助。
  • 很高兴它有帮助。确保在 ajax 调用后将 jQuery.support.cors 设置回 false 以防止不需要或意外的 XSS。
猜你喜欢
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-21
相关资源
最近更新 更多