我有一个小问题。当我打电话时
myVar = $.post('/check_2/', JSON.stringify({"newname": window.NEWNAME,}), callback, 'json')
在“myVar”中我有一个对象,因此,当我执行 console.log myVar
时,我得到如下内容:
Object {readyState: 1, setRequestHeader: function, getAllResponseHeaders: function,getResponseHeader: function, overrideMimeType: function…}
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}
[...]
如果我制作 console.log myVar.success
(例如),它会打印成功时显示的内容,但是,如果我制作 console.log myVar.responseText
(调用的结果是),我不断得到undefined
,所以我永远无法真正访问我感兴趣的数据。
有什么想法可以访问这些数据吗?
我知道我对后调用有一些误解,但由于我有误解,所以我不知道我做错了什么。
我使用post而不是get,因为我确实需要向后端发送数据,这样我可以在数据库中进行一些检查
编辑: 我在哪里制作console.log:
check2: (callback) ->
console.log "Starting..."
myVar = $.post('/check_2/', JSON.stringify({"newname": window.NEWNAME,}), callback, 'json')
console.log myVar
console.log "success example"
console.log myVar.success
console.log "responseText"
console.log myVar.responseText
编辑2
这是 console.log myVar
显示的对象的照片
最佳答案
$.post
是一个AJAX调用,它不返回服务器的响应,它返回一个jqXHR
promise :
jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )
Returns: jqXHRDescription: Load data from the server using a HTTP POST request.
如果您想要来自 AJAX 调用的数据,那么您必须从回调中获取它:
fn = (data, status, jqxhr) ->
# Your data is in `data` so do what you
# need to do with `data` around here
...
# And then call the other `callback` function
# by hand.
callback(data, status, jqxhr)
$.post('/check_2/', JSON.stringify({"newname": window.NEWNAME,}), fn, 'json')
关于javascript - CoffeeScript $.post 从返回对象获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20354823/