简体   繁体   English

未触发从 AJAX 回调返回的 JSON

[英]JSON return from AJAX callback not fired

My AJAX function (in coffeescript) is returning values, but neither the error nor the success callback are fired.我的 AJAX 函数(在 coffeescript 中)正在返回值,但errorsuccess回调都没有被触发。

$ ->
  $('#sf_field').autocomplete
    source: (request, response) ->
      $.ajax
        url: 'https://XXXXX.my.salesforce.com/services/data/v48.0/search/suggestions'
        type: 'GET'
        contentType: 'application/json'
        cache: false,        
        crossDomain: true
        dataType: 'json'
        beforeSend: (xhr) ->
          xhr.setRequestHeader 'Authorization', 'Bearer XXXX.XXXXX'
          return
        data: {
            q:  request.term,
            sobject: 'Contact'
          }
    success: (data) ->
      console.log "success"
      alert "success"
      json = $.parseJSON(data)
    error: () ->
      console.log "error"
      alert "error"
    complete: () ->
      console.log "complete"
      alert "complete"
    select: (event, ui) ->
      $("#sf_id").val(ui.item.id)
  return

I see in the firefox console, the XHR results are there in correct JSON format (validated on jsonlint) , but the callbacks seem not be fired, I can't see the suggestions in the UI (not even the log messages in the console do appear).我在 firefox 控制台中看到,XHR 结果以正确的 JSON 格式存在(在 jsonlint 上验证),但回调似乎没有被触发,我看不到 UI 中的建议(甚至控制台中的日志消息都没有出现)。

The resulting JSON from the browsers XHR-response window:来自浏览器 XHR 响应窗口的结果 JSON:

{
"autoSuggestResults":[{"attributes":{"type":"Contact",
"url":"/services/data/v48.0/sobjects/Contact/XXXXAU"},
"Id":"XXXXXX01uQoVQAU",
"Name":"Martin Aaaaaa"},
{"attributes":{"type":"Contact",
"url":"/services/data/v48.0/sobjects/Contact/XXXXuQoZQAU"},
"Id":"XXXXXZQAU",
"Name":"Martin Bbbbbb"},
{"attributes":{"type":"Contact",
"url":"/services/data/v48.0/sobjects/Contact/XXXXojQAE"},
"Id":"XXXXXojQAE",
"Name":"Martin Cccccc"},
{"attributes":{"type":"Contact",
"url":"/services/data/v48.0/sobjects/Contact/XXXXQn5QAE"},
"Id":"XXXXXQn5QAE",
"Name":"Thomas Martin Dddddd"},
{"attributes":{"type":"Contact",
"url":"/services/data/v48.0/sobjects/Contact/XXXXQv7QAE"},
"Id":"XXXXX0001uQv7QAE",
"Name":"Martin Eeeeee"}],
"hasMoreResults":true,
"meta":{"nameFields":[{"entityApiName":"Contact",
"fieldApiName":"Name"}],
"secondaryFields":[]}
}

I have tried with various dataType: (jsonp, text, html), but this doesn't help Thanks for your hints我尝试过各种数据类型:(jsonp、text、html),但这没有帮助感谢您的提示

As suggested by PGill, The intendation was the problem.正如 PGill 所建议的,意图是问题所在。 Therefore, my coffeescript should look like:因此,我的咖啡脚本应该如下所示:

$ ->
  $('#sf_field').autocomplete
    source: (request, response) ->
      $.ajax
        url: 'https://XXXXX.my.salesforce.com/services/data/v48.0/search/suggestions'
        type: 'GET'
        contentType: 'application/json'
        cache: false,        
        crossDomain: true
        dataType: 'json'
        beforeSend: (xhr) ->
          xhr.setRequestHeader 'Authorization', 'Bearer XXXX.XXXXX'
          return
        data: {
            q:  request.term,
            sobject: 'Contact'
          }
        success: (data) ->
          console.log "success"
          alert "success"
          json = $.parseJSON(data)
        error: () ->
          console.log "error"
          alert "error"
        complete: () ->
          console.log "complete"
          alert "complete"
        select: (event, ui) ->
          $("#sf_id").val(ui.item.id)
  return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM