Javascript 复制jquery ajax-为什么是403?[CORS]
我尝试模拟下面的函数。它是从immowelt.de装载的。但是js文件位于 模拟功能:Javascript 复制jquery ajax-为什么是403?[CORS],javascript,html,jquery,Javascript,Html,Jquery,我尝试模拟下面的函数。它是从immowelt.de装载的。但是js文件位于 模拟功能: a.ajax({ type: "POST", url: IwAG.Vars.acSource ? IwAG.Vars.acSource : j, contentType: "application/json; charset=utf-8", dataType: "json",
a.ajax({
type: "POST",
url: IwAG.Vars.acSource ? IwAG.Vars.acSource : j,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{ "inputText": "' + c.term + '", "limit": 150, "geoID": "' + h + '", "region": "' + g + '" }',
success: function(c) {
if (c.response && c.response != "")
b(a.map(c.response, function(a) {
return {
label: a.label,
fragment: a.fragment,
value: a.value,
district: a.indent == true,
fullLabel: a.fullLabel
}
}));
else
b("")
}
})
模拟:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script><!-- "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --disable-gpu --user-data-dir=~/chromeTemp -->
$(document).ready(function() {
$("button").click(function(){
$.ajax({
type: "POST",
url: "https://media-static.immowelt.org/_scripts/mvc/bundles/mvcroot/search/GetSuggestionList",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: '{ "inputText": "' + "M" + '", "limit": 150, "geoID": "' + "108" + '", "region": "' + "0" + '" }',
success: function(c) {
window.alert("sometext");
if (c.response && c.response != ""){}
else{};
}
});
});
});
</script>
</head>
<body>
<p>click this:</p>
<button>Get External Content</button>
</body>
</html>
您点击了错误的URL: URL应该是 主要问题是CORS您不能在您自己的域中运行它,因为在服务器端没有允许所有人都使用的头集 如果要测试以下代码,请转到 并将下面的代码粘贴到chrome控制台中,它工作正常 jQuery.ajax{ 类型:POST,, 网址:https://www.immowelt.de/mvcroot/search/GetSuggestionList, contentType:application/json;字符集=utf-8, 数据类型:json, 数据:{inputText:'+M+',极限:150,大地水准面:'+108+',区域:'+0+'}', 成功:功能C{ window.alertsometext; 如果c.response&&c.response!={} else{}; }
};403错误的原因是您没有发送头。由于您正在发出CORS请求,因此无法发送任何自定义标头,除非服务器通过向响应中添加访问控制允许标头来启用这些标头。403错误中有更多详细信息吗?它提到CORS吗?请在控制台和/或网络选项卡中显示完整的错误消息。它将显示请求的URL以及403是否由服务器或浏览器引起。
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --disable-gpu --user-data-dir=~/chromeTemp"