微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Ajax jquery调用获取NetworkError:403 Forbidden错误响应

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

如果我从同一个容器运行相同的脚本,我将从Web服务获得有效的响应.

我正在使用以下代码.

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

请建议.

解决方法

因为webserver假设它跨域通信,这就是为什么你得到403.

你需要使用JSONP

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

基本用法.

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

              },error: function(xOptions,textStatus){

              }
        });

      },textStatus){

      }
});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐