2012-07-20 56 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将自动检测它。

文档

相关问题