外部网站吐出正确的 JSON,代码太简单,我不知道哪里出了问题。我尝试过 get 和 post。
$(document).ready(function() {
$.get("https://www.w3schools.com/jquery/demo_ajax_json.js", function(result) {
alert(result);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
最佳答案
如果您尝试从本地主机访问,请附加此行 http://cors-anywhere.herokuapp.com/否则保持像 https://www.w3schools.com/jquery/demo_ajax_json.js然后使用 $.getJson ,它会输出你需要的结果,同时 JSON.stringify JSON 数据,以便你可以在警报中查看数据,否则 [Object][Object] 将被警报
var resultUrl = 'http://cors-anywhere.herokuapp.com/'+'https://www.w3schools.com/jquery/demo_ajax_json.js';
$.getJSON(resultUrl, function(result) {
alert(JSON.stringify(result));
});
关于javascript - 极其简单的Ajax代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47298526/