Jquery 如何在函数中编写ajax并返回其值
我试图在函数中编写ajax并返回其值,然后根据值做一些事情Jquery 如何在函数中编写ajax并返回其值,jquery,ajax,coffeescript,Jquery,Ajax,Coffeescript,我试图在函数中编写ajax并返回其值,然后根据值做一些事情 check_email_present = (email) -> $.ajax({ type: "GET", url: "/donor/check_email_present", data: { email: email }, success:(data) -> return data error:(data) -> return false })
check_email_present = (email) ->
$.ajax({
type: "GET",
url: "/donor/check_email_present",
data: { email: email },
success:(data) ->
return data
error:(data) ->
return false
})
我把这个函数叫做
return_value = check_email_present(email)
console.log(return_value)
我可以在日志中看到对象,当它试图打印return_value.responseText时,我没有定义
我还尝试了console.log(JSON.parse(返回值))
在success函数中,我可以这样做(使用if条件检查数据),但我希望将数据返回到变量,然后检查它,不要尝试从异步函数返回内容。而是使用回调:
check_email_present = (email, done) ->
$.ajax({
type: "GET",
url: "/donor/check_email_present",
data: { email: email },
success:(data) ->
done(data)
error:(data) ->
done(null)
})
然后:
check_email_present(email, return_value -> console.log(return_value))
成功中的数据内容应该是什么?举个例子?如果响应是JSON数据,您可以在Ajax请求中添加数据类型参数,如
dataType:JSON“,
I返回一个普通字符串值@SherinJose