我正在使用数据 var result = $ .getJSON。
当我在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(结果['statusText'],但两者都返回为undefined。给出了什么?
编辑:这是我正在使用的实际代码。
$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;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 :(得分: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
}
});
你说答案是文字“ok”;我建议你不要使用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会自动检测它。
<强>文档强>
jQuery.getJSON
- http://api.jquery.com/jQuery.getJSON/ jQuery.ajax
- http://api.jquery.com/jQuery.ajax/ 答案 1 :(得分:0)
这很简单:
result.statusText
答案 2 :(得分:0)