Ajax jquery call getting NetworkError: 403 Forbidden error in response

女生的网名这么多〃 提交于 2020-01-02 05:43:07

问题


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.

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.


回答1:


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

You need to use JSONP for this

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){

      }
});


来源:https://stackoverflow.com/questions/11185285/ajax-jquery-call-getting-networkerror-403-forbidden-error-in-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!