jquery ajax ie9 出错,IE9 JQuery.ajax引发了神秘错误(IE9 JQuery.ajax throws mystery error)

I'm experiencing a difficult ajax error in IE9. I will say up-front that this is not technically a cross-domain request so I'm sure that is not the problem.

Edit: It appears that the problem may be related to a same-domain request being treated as cross-domain, though it is not clear why (see comments).

I'm using a local proxy to make cross-domain ajax requests, and the url I use looks like http://localhost/proxy/?target=..... All good browsers are happy with this but IE immediately returns an error before the request is even dispatched (I have verified this using Fiddler and IE's developer tools network capture).

When I inspect the error object passed to JQuery's ajax.error callback its isRejected() function returns true, but I can't find out what would cause a request to be rejected. The error object's readyState is 4 (implying the request is complete?), status is 0 and statusText is "error".

I'm building my request a bit like this:

$.ajax(url, {

data: {...list of parameters...},

dataType: 'json',

type: 'GET',

success: function() { ... },

error: function() { ... },

beforeSend: proxy.beforeRequestSend,

timeout: 35000

});

The proxy.beforeRequestSend function looks like this:

this.beforeRequestSend = function(XHR, settings) {

if(that.proxyRequired(settings.url)) {

// I've also tried using the full url (http://localhost/proxy/?...)

settings.url = 'proxy/?target=' + encodeURIComponent(settings.url);

}

};

Does anyone know what would cause the request to fail outright in IE9? Unfortunately I don't have access to IE8 or 7 for testing but I assume they would behave the same. The fact that JQuery's ajax error handler is invoked suggests that the error is not occuring while I am building the request, but when it is supposedly executed. All other browsers working correctly tells me that the fundamentals are correct.

I have previously read that the JQuery / IE combo has issues url-encoding some characters, but if I alert settings.url at the end of the beforeSend function the target URL is correctly encoded (this also tells me that that.proxyRequired(settings.url) returns true).

Any input much appreciated.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用Servlet、jQuery和JSON格式数据实现表单的Ajax验证的示例代码: HTML代码: ```html <form id="myForm" action="#" method="post"> <label for="username">用户名:</label> <input type="text" id="username" name="username"><br> <label for="password">密码:</label> <input type="password" id="password" name="password"><br> <input type="submit" value="提交"> </form> ``` jQuery代码: ```javascript $(function() { $('#myForm').submit(function(event) { event.preventDefault(); // 阻止表单的默认提交行为 var formData = $(this).serialize(); // 将表单数据序列化为字符串 $.ajax({ type: 'POST', url: 'validate', // 后台处理验证的Servlet data: formData, dataType: 'json', // 声明返回的数据类型为JSON success: function(data) { // 请求成功时的回调函数 if (data.status === 'success') { alert('验证通过!'); } else { alert('验证失败:' + data.message); } }, error: function(jqXHR, textStatus, errorThrown) { // 请求失败时的回调函数 alert('请求失败:' + textStatus + ',错误信息:' + errorThrown); } }); }); }); ``` Servlet代码: ```java import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONObject; @WebServlet("/validate") public class ValidationServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); PrintWriter out = response.getWriter(); String username = request.getParameter("username"); String password = request.getParameter("password"); if (validate(username, password)) { JSONObject result = new JSONObject(); result.put("status", "success"); out.print(result.toString()); } else { JSONObject result = new JSONObject(); result.put("status", "fail"); result.put("message", "用户名或密码错误"); out.print(result.toString()); } out.flush(); out.close(); } private boolean validate(String username, String password) { // 模拟验证用户名和密码的函数 if (username.equals("admin") && password.equals("123456")) { return true; } else { return false; } } } ``` 以上代码实现了一个简单的表单验证功能,当用户点击提交按钮时,jQuery会将表单数据通过Ajax方式提交给后台Servlet进行验证。后台Servlet根据表单数据进行验证,并将验证结果以JSON格式返回给前端页面,前端页面根据返回数据进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值