Ajax jquery调用获取NetworkError:403 Forbidden错误响应
发布时间:2020-12-16 02:48:30 所属栏目:百科 来源:网络整理
导读:我使用apache tomcat作为Web服务器. 我已经在tomcat上部署了webservices. 如果我通过 jquery ajax从本地文件系统发送请求到tomcat webservice作为响应我收到403错误. 如果我从同一个容器运行相同的脚本,我将从Web服务获得有效的响应. 我正在使用以下代码. fu
|
我使用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){
}
});
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
