我有以下功能,当前仅显示从GET请求返回的对象到控制台。我需要做的是解析“数据”对象,以便可以将数据中的“输出”字符串返回到我的网页。任何帮助将不胜感激。
window.onload = function() {
var output = $.ajax({
url: 'https://ajith-holy-bible.p.mashape.com/GetVerseOfaChapter?Book=John&chapter=3&Verse=16', // The URL to the API. You can get this by clicking on "Show CURL example" from an API profile
type: 'GET', // The HTTP Method, can be GET POST PUT DELETE etc
data: {}, // Additional parameters here
dataType: 'json',
success: function (data) {
//
//Change data.source to data.something , where something is whichever part of the object you want returned.
//To see the whole object you can output it to your browser console using:
console.log(data);
},
error: function (err) {
alert(err);
},
beforeSend: function (xhr) {
xhr.setRequestHeader("X-Mashape-Authorization", "my_key_here"); // Enter here your Mashape key
}
});
}
我不确定您的JSON的样子,但可以说是
{
"foo": "some value",
"bar": "some other value"
}
那么您可以执行以下操作:
success: function (data) {
console.log(data);
var dataObj = $.parseJSON(data);
$.each(dataObj, function(key, value) {
if(key == "foo") {
// do something with foo's value
console.log(value);
}
if(key == "bar") {
// do something with bar's value
console.log(value);
}
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句