Ajax jquery 调用获取 NetworkError: 403 Forbidden error in response

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11185285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:18:45  来源:igfitidea点击:

Ajax jquery call getting NetworkError: 403 Forbidden error in response

jqueryajaxweb-servicestomcat

提问by Chetan Shirke

I am using apache tomcat as a web server. I have deployed webservices on tomcat. If i post request through jquery ajax from local file system to tomcat webservice in response i am getting 403 error.

我使用 apache tomcat 作为网络服务器。我已经在 tomcat 上部署了 web 服务。如果我通过 jquery ajax 从本地文件系统向 tomcat webservice 发送请求作为响应,我会收到 403 错误。

If i run the same script from the same container i am getting valid response from the webservice.

如果我从同一个容器运行相同的脚本,我会从网络服务获得有效响应。

I am using following code.

我正在使用以下代码。

function callservice() 
    {
    jQuery.support.cors = true;
        var mobNo = document.getElementById('mobileNo').value;
        var acctNo = document.getElementById('accountNo').value;
        //var id = document.getElementById('id').value;
        var custNo = document.getElementById('customerId').value;

        //var mobNo = document.getElementById('txt_name').value;
        //alert("mobileNo" + mobNo+"accountNo" + acctNo+"customerId "+custNo);

        var url = "http://localhost/mobile-services/rest/user/";        
        var dataVal = {};
        dataVal["mobileNo"] = mobNo;
        dataVal["accountNo"] = acctNo;
        dataVal["customerId"] = custNo;

        var forminput = JSON.stringify(dataVal);

    $.ajax({
        url: url,
        type: "POST",
        data:forminput,
        processdata: true,
        contentType: "application/json;",
        beforeSend: function () { },
        headers : 
        {
            "Content-Type"  : "application/json",
            "Accept" : "application/json"               
        },
        success: function (data) 
        {          
            if (data.authenticated==true)
            {
                window.location.replace("http://localhost:8080/mobile-services/selected_services.jsp?userId="+data.id);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) 
        {
            try 
            {
                alert(JSON.stringify(XMLHttpRequest) + "\n" + textStatus + "\n" + errorThrown);
            }
            catch (ex) { alert("Exception occured.. "); }
            finally { }
        }
    });
}

Please suggest.

请建议。

采纳答案by Zahid Riaz

Because webserver is assuming it cross domain communication thats why you are getting 403.

因为网络服务器假设它是跨域通信,这就是您收到 403 的原因。

You need to use JSONP for this

您需要为此使用 JSONP

https://github.com/jaubourg/jquery-jsonp

https://github.com/jaubourg/jquery-jsonp

basic usage.

基本用法。

$.jsonp({
      var url  = "abc.do";
      success: function(jsonData, textStatus) {
          $.jsonp({
              url: url+"?callback=?",
              success: function(jsonData, textStatus) {

              },
              error: function(xOptions, textStatus){

              }
        });

      },
      error: function(xOptions, textStatus){

      }
});