如何在函数中编写ajax并返回其值

时间:2016-01-04 07:31:10

标签: 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
  })

我将此功能称为

  return_value = check_email_present(email)
  console.log(return_value)

我可以在日志中看到该对象,当它尝试打印return_value.responseText时,我得到了未定义。

我也尝试过console.log(JSON.parse(return_value))

在成功功能中,我能够做到(用if条件检查数据),但我想将数据返回给变量,然后检查它

1 个答案:

答案 0 :(得分:0)

不要尝试异步函数中的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))

然后:

select a.NPP, a.NAMA, a.JOB, a.STATUS, a.last_updated, 
a.UNIT_BESARAN, a.PERIODE, a.GOAL_ID,a.USER_APPROVED, 
a.DATE_APPROVED, a.USER_SUBMITTED, a.DATE_SUBMITTED 
from (select a.NPP, a.NAMA, b.job, b.status, to_char(b.last_updated, 'dd-MM-yyyy HH24:MI:SS') AS last_updated, 
b.periode , b.goal_id, b.USER_APPROVED, b.DATE_APPROVED, b.USER_SUBMITTED, b.DATE_SUBMITTED, 
case when b.UNIT_BESARAN is null then a.UNIT_BESARAN else b.UNIT_BESARAN end UNIT_BESARAN 
from OL_PEGAWAI_DAILY a left join PFM_GOAL_HEADER b on a.PERSON_ID=b.PERSON_ID and b.last_status=1 and 
b.periode = '2015' and b.status is null order by b.STATUS asc) a where UNIT_BESARAN like 'DIVISI SOLUSI % KEAMANAN TEKNOLOGI INFORMASI' 
and a.status is null and npp not in (

--here subquery
select f.NPP from (select f.NPP, g.status, g.goal_id, 
case when g.UNIT_BESARAN is null then f.UNIT_BESARAN else g.UNIT_BESARAN end UNIT_BESARAN 
from OL_PEGAWAI_DAILY f left join PFM_GOAL_HEADER g on f.PERSON_ID=g.PERSON_ID and g.last_status=1  and 
g.periode = '2015' and g.status='2' and g.job is not null and g.last_updated is not null order by g.STATUS asc) f 
where UNIT_BESARAN like 'DIVISI SOLUSI %' and f.status is not null order by goal_id asc

) order by goal_id asc