我正在尝试使用JQuery使用本地URL,但它只是回来了 [对象]。你能帮我吗?
代码:
$(document).ready(function() {
$("button").click(function() {
$.getJSON(http: "//localhost:8082/pessoa/ellem", function(result) {
$.each(result, function(i, field) {
$("p").append(field + " ");
});
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button> JSON </button>
我用另一个链接测试了它,例如:https://vimeo.com/api/oembed.json?url=http://vimeo.com/13211055, 它工作正常,我不知道还能做什么,帮助我!
答案 0 :(得分:-1)
问题是对象值是对象。在link you provided中,所有对象值都是字符串或整数,因此您的代码可以正常工作:
{
type: "video",
version: "1.0",
...
}
在这里,你可以看到它是如何工作的。当您尝试将对象转换为字符串时,它将显示为[object object]
。
$(document).ready(function(){
var result = {test: "testing", test2: {obj: 'test'}};
$.each(result, function(i, field){
$("p").append(field + " ");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>
您需要以递归方式遍历每个字段(如下所示),或者只显示您想要的内容:
$(document).ready(function(){
var result = {test: "testing", test2: {obj: 'test'}};
var appendObj = function(obj) {
$.each(obj, function(i, field){
if ($.isPlainObject(field)) {
//We're dealing with another object, loop through it, and echo it's contents
appendObj(field);
} else {
//Just a regular field, append like normal.
$("p").append(field + " ");
}
});
};
//Start the appending with the main result.
appendObj(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>