Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在函数中编写ajax并返回其值_Jquery_Ajax_Coffeescript - Fatal编程技术网

Jquery 如何在函数中编写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 })

我试图在函数中编写ajax并返回其值,然后根据值做一些事情

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