gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:49:46 26 4
gpt4 key购买 nike

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

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

我正在使用以下代码。

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

请提出建议。

最佳答案

因为网络服务器假设它是跨域通信,这就是您收到 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){

}
});

关于jquery - Ajax jquery 调用获取 NetworkError : 403 Forbidden error in response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11185285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com