我正在这里测试代码:get
以下是代码:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
var processJSON = function(data, textStatus, xhr) {
alert(xhr.status);
}
// for whatever reason, the following URL is not working any more, so you won't be able to test it anymore.
var myURL='https://ckeqt3eoea.execute-api.us-east-1.amazonaws.com/pettest/test?name=223';
// var myURL="https://jsonplaceholder.typicode.com/users"
$("button").click(function(){
$.ajax({
url: myURL,
dataType: "json",
contentType: 'application/json',
success: processJSON
});
});
});
</script>
</head>
<body>
<button>Send Request</button>
</body>
</html>如代码所示,我正试图解析来自这个URL:https://ckeqt3eoea.execute-api.us-east-1.amazonaws.com/pettest/test?name=223的响应
您可以直接访问该URL,并发现来自AWS-API-Gateway的响应是:
{
"cc":"dd",
"name":"ee"
}我能够使用上面的javascript解析来自其他来源的其他json响应。但是我正在努力分析来自AWS-API-Gateway的上述响应。如果取消对var myURL第二行的注释,您将看到代码实际上只适用于其他URL。
==========
针对现有的答复:
json和jsonp。两者都适用于其他URL(包括我评论的URL)。但这两种方法都不适用于AWS网关API。发布于 2016-08-10 23:50:14
是数据类型。您正在告诉jQuery期待jsonp回调。您要寻找的是dataType:"json“。
更新
我刚测试了你的密码。问题是,您没有在您的宠物/测试资源中定义的OPTIONS方法。使用API控制台,打开测试资源(假设test是工作台,测试是资源),然后使用"Actions“下拉按钮启用CORS。这将自动创建OPTIONS方法,并在GET方法中设置所需的标头。
发布于 2016-08-10 22:38:58
和你的API一起为我工作。也许无关紧要,但我没有使用'resp‘作为变量标签,而是使用了'data’。
另外,调用一个命名函数,而不是将函数嵌入到内联中。
function processJSON(data) {
alert(data.name);
}
dataType: "json",
contentType: 'application/json',https://stackoverflow.com/questions/38841878
复制相似问题