我以爲我得到的文檔URI是一個JSON字符串,並將其正確地轉換爲對象,但我的代碼似乎沒有工作,並給我就像它在各種元素上返回null一樣。
我的JavaScript得到傳遞給網頁的參數,再
<script type="text/javascript">
// ==UserScript==
// @name Enable alert()s
// @match //jsfiddle.com/*
// @require http://code.jquery.com/jquery-latest.min.js
// @grant unsafeWindow
// ==/UserScript==
this.$ = this.jQuery = jQuery.noConflict(true);
$(document).ready(function() {
var QueryString = function() {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]));
}
}
return query_string;
}();
//we could use this on the dom somewhere too!!!!!!! to lable what we are looking at! :)
theFile=QueryString.b;
theFolder = QueryString.a;
theJson = $.getJSON(QueryString.c);
console.log(QueryString.c);
$("p").text("You are viewing: " + theJson.name);
//the value for theJson.name is undefined :(
});
</script>
像theJson.name物品的價值是不確定的,但是這是一個打印到控制檯的字符串的結果:
{"name":"PDS_03962670","cdate":"2014-06-12","test":"test2"}
對我來說,這應該工作,但不是,不知道我失蹤了。如果上述#.getJSON()調用之前的字符串是正確的,那麼不知道我在我的console.log做錯了
注意'.getJson'基本上是.ajax'的'的簡寫版本,如在[文檔](HTTP解釋://api.jquery。 COM/jquery.getjson /)。所以你*真的*需要閱讀和理解這個騙局。 –