简体   繁体   English

jQuery Cors Ajax请求失败

[英]jQuery cors ajax request failing

I'm trying to fire a cors ajax request with jQuery towards a different port in the same machine, below is my code (my application is running on an appache server on port 80): 我正在尝试使用jQuery向同一台机器中的其他端口触发cors ajax请求,以下是我的代码(我的应用程序在端口80的appache服务器上运行):

JS: JS:

$(function(){
    jQuery.support.cors = true;
    $.ajax({
        type: "POST",
        url: "http://127.0.0.1:8080/sys_monitor/ccn",
        dataType: "text",
        contentType: "text/plain",
        data : {a: '11'},
        success : function(res){
            alert(res);
            $('#stage').html(res);
        },
        error : function(jqXHR, text, exception){
            if (jqXHR.status === 0) {
                alert ('Not connected.\nPlease verify your network connection.');
            } else if (jqXHR.status == 404) {
                alert ('The requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert ('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert ('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert ('Time out error.');
            } else if (exception === 'abort') {
                alert ('Ajax request aborted.');
            } else {
                alert ('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });
        });

Servlet ("post" is written to the console) running on tomcat (eclipse) on port 8080: 在端口8080的tomcat(eclipse)上运行的Servlet(将“ post”写入控制台):

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().write("hello there");
    System.out.println("post");
}

using firebug I can see the request (with response code 200) and content length 11 (empty result tab), but the error function is being executed with XHR status 0 使用firebug,我可以看到请求(响应代码为200)和内容长度为11(空结果选项卡),但是错误功能正在以XHR状态0执行

(side question1: why is firebug displaying "xml" as type ?): (旁边的问题1:萤火虫为什么将“ xml”显示为类型?):

(side question2: why isn't there an OPTIONS request before the POST ?): (旁边的问题2:为什么在POST之前没有OPTIONS请求?):

萤火虫

I also tried with RESTClient plugin for firefox, and I'm getting the wanted result: 我也尝试过使用Firefox的RESTClient插件,并且得到了想要的结果:

RESTClient插件

all help/tips is welcome 欢迎所有帮助/提示

Check this question XmlHttp Request status 0, localhost issues (javascript, ajax, php) help , 检查此问题XmlHttp请求状态0,本地主机问题(javascript,ajax,php)帮助

it looks like a browser issue dealing with localhost 看起来像是处理本地主机的浏览器问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM