2012-07-20 122 views
0

我使用的數據是 var result = $ .getJSON。如何通過javascript中的鍵獲取對象的值?

當我console.log(結果);我得到這個對象:

Object 
    abort: function (a){a=a||"abort",p&&p.abort(a),w(0,a);return this} 
    always: function(){i.done.apply(i,arguments).fail.apply(i,arguments);return this} 
    complete: function(){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this} 
    done: function(){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this} 
    error: function(){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this} 
    fail: function(){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this} 
    getAllResponseHeaders: function(){return s===2?n:null} 
    getResponseHeader: function (a){var c;if(s===2){if(!o){o={};while(c=bH.exec(n))o[c[1].toLowerCase()]=c[2]}c=o[a.toLowerCase()]}return c===b?null:c} 
    isRejected: function(){return!!e} 
    isResolved: function(){return!!e} 
    overrideMimeType: function (a){s||(d.mimeType=a);return this} 
    pipe: function (a,b,c){return f.Deferred(function(d){f.each({done:[a,"resolve"],fail:[b,"reject"],progress:[c,"notify"]},function(a,b){var c=b[0],e=b[1],g;f.isFunction(c)?i[a](function(){g=c.apply(this,arguments),g&&f.isFunction(g.promise)?g.promise().then(d.resolve,d.reject,d.notify):d[e+"With"](this===i?d:this,[g])}):i[a](d[e])})}).promise()} 
    progress: function(){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this} 
    promise: function (a){if(a==null)a=h;else for(var b in h)a[b]=h[b];return a} 
    readyState: 4 
    responseText: "{'result':'success'}" 
    setRequestHeader: function (a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this} 
    state: function(){return e} 
    status: 200 
    statusCode: function (a){if(a){var b;if(s<2)for(b in a)j[b]=[j[b],a[b]];else b=a[v.status],v.then(b,b)}return this} 
    statusText: "OK" 
    success: function(){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this} 
    then: function (a,b,c){i.done(a).fail(b).progress(c);return this} 
__proto__: Object 

如何獲取statusText鍵的值? (在這種情況下「OK」); 。

我試過的console.log(result.statusText)和執行console.log(結果[「狀態文本」],但兩者回報不確定是什麼讓

編輯:下面是我使用的實際代碼。

$j(".buyNowButton").click(function(){ 
     var qty = $j(this).attr('qty'); 
     var product_id = $j(this).attr("product_id"); 

     var params = "product_id=" + product_id + "&amp;amp;amp;amp;amp;amp;amp;amp;amp;amp;qty=" + qty;   

     var result = $j.getJSON("/acumen/scripts/addToCart.php", params, function(data, textStatus){ 
      console.log(textStatus); 
      if (textStatus == "error"){ 
       alert("There was an error adding this item to your cart. Please call customer service for assistance.", "Error"); 
       return; 
      }; 
      if (data.result == "error"){ 
       alert("Sorry, an error occurred while adding the item to your cart. The error was: '" + data.message + "'"); 
       return; 
      }; 
     }); 
     console.log(result); 
}); 
+0

事實上,這是'result.statusText'。你將不得不發佈你正在使用的代碼來幫助任何人。 – Pointy 2012-07-20 17:22:08

+0

https://developer.mozilla.org/zh/JavaScript/Guide/Working_with_Objects – jbabey 2012-07-20 17:27:33

回答

1

這聽起來像你沒有正確使用對象當您使用console.log(result),唯一的原因,你所看到的結果文本是因爲。 Firebug控制檯更新參考值。如果您嘗試在同一行代碼中訪問result.statusText,則不會得到結果,因爲該請求可能尚未實際完成。

這條線:

var result = $.getJSON 

是給你的請求對象本身。如果你要處理的反應,你在一個回調函數這樣做:

$.getJSON('request/url/goes/here', request_data, function(result) { 
    // this is where you do something with your json data 
}); 

...理解是getJson是一種「快速別名」這樣做的:

$.ajax({ 
    url:'request/url/goes/here', 
    dataType: 'json', 
    data: request_data, 
    success: function (result) { 
     // this is where you do something with your json data 
    } 
}); 

你說答案是文字「好」;那麼我會建議你不要使用getJson,因爲它預計響應是json數據。相反:

$.ajax({ 
    url:'request/url/goes/here', 
    data: request_data, 
    success: function (result) { 
     // this is where you do something with your response string "ok" 
    } 
}); 

..請注意,我沒有指定回調的數據類型爲json。 jQuery將自動檢測它。

文檔

相關問題