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){ } }); }, error: function(xOptions, textStatus){ } });