我试图用JSON跨域抓取创建一个基本测试.我已经准备好了访问控制-允许-原产地,并且被浏览器识别,甚至是JSON输出,但是不管出于什么原因,Jquery都不会使用这些内容。没有控制台错误,检查网络返回,一切都是fine...not确定是怎么回事。
以下是我的html代码:
<head>
<title>Hello jQuery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url: "http://myurlremoved:)"
}).then(function(data) {
$('.greeting-id').append(data.id);
$('.greeting-content').append(data.content);
});
});
</script>
</head>
<body>
<div>
<p class="greeting-id">The ID is </p>
<p class="greeting-content">The content is </p>
</div>从这里开始,我只是通过一个假端点输出简单的静态JSON:
{"id":439,"content":"Hello, World!"} 我在这件事上来来回回.我不知道是怎么回事。有人看到我失踪的明显情况了吗?浏览器显示标题允许通过跨域头中的域通配符跨域.我不确定还有什么是wrong...but --这对我来说是新的。
发布于 2016-02-14 01:00:04
返回的结果是json对象的字符串,而不是实际对象。您必须做的是以下之一:
$.getJSON方法而不是ajax方法。parseJSON变量使用data。dataType: "json"添加为ajax请求中url下面的选项之一。试一试
<!DOCTYPE html>
<html>
<head>
<title>Hello jQuery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url: "http://framework.testlocker.net/endpoints/testrest.cfm",
dataType:"json"
}).then(function(data) {
$('.greeting-id').append(data.id);
$('.greeting-content').append(data.content);
});
});
</script>
</head>
<body>
<div>
<p class="greeting-id">The ID is </p>
<p class="greeting-content">The content is </p>
</div>
</body>
</html>输出:

https://stackoverflow.com/questions/35387081
复制相似问题