ajax - 即使服务器返回成功,Ajax调用也总是触发失败处理程序

标签 ajax google-apps-script error-handling

即使从服务器端返回成功值,以下JavaScript也会始终触发失败处理程序:

$.ajax(payload)
    .done(function(data, statusText, jqxhr) {
            document.getElementById('myModal').innerHTML = "<p>Record Saved ... </p>";
            modal.style.display = "block";
            refresh_html_page(document.getElementById("sheetname").value);
    })
    .fail(function(jqxhr, statusText, errorThrown) {
            document.getElementById('myModal').innerHTML = "<p>Record Not Saved ... </p>";
            modal.style.display = "block";  
            refresh_html_page(document.getElementById("sheetname").value);
    })
    .always(function () {
    // Re-enable the inputs
    $inputs.prop("disabled", false);
    });

返回的JSON字符串:

[{“结果”:“成功”,“行”:11}]

有什么想法吗?

最佳答案

好消息。我能够破解它。解决方法如下:

  • 在有效载荷
  • 中设置回调函数
  • 在新创建的回调函数
  • 中进行虚拟操作
  • 在创建jasonp响应
  • 时在服务器端给回调函数名称加上前缀

    客户端:
    function handleJSONPResponse(data, status, request) {
       console.log('response', data);
    }
    
    
    // Fire off the request to /form.php
    var payload = {
         crossDomain: true,
         url: "https://script.google.com/macros/s/XXXXXXXXXXXXXXXXX/exec",
         method: "POST",
         dataType: "jsonp",
         data: serializedData,
         jsonpCallback: 'handleJSONPResponse'
    };
    

    服务器端(e是从客户端发送的有效负载):
    return ContentService
          .createTextOutput(e.parameters.callback + '(' + JSON.stringify({"result":"success", "row": nextRow})+ ')')
          .setMimeType(ContentService.MimeType.JAVASCRIPT);
    

    解决问题真是太好了。非常感谢您的投入和鼓励。非常感激。

    关于ajax - 即使服务器返回成功,Ajax调用也总是触发失败处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49843803/

    相关文章:

    javascript - 使用直接在我的 wordpress 页面中嵌入 javascript 的 ajax(和 jquery)从远程服务器 php 文件获取一个简单的 JSONP 对象

    php - 我正在尝试使用 ajax 从数据库中检索数据并以 Bootstrap 模式中的形式填充

    arrays - 比较两个数组以在 Google 应用程序脚本上找到相等值的最快方法

    arrays - 如何在 Google Sheet 的 App Script 中创建数组的真正新副本?

    excel - 如何处理 VBA 中的 DLL 错误?

    php - 尝试访问非对象(未分配)的属性时没有错误或警告

    php - json_decode 为 WordPress 中的 POST 数据返回 "NULL"

    php - .post 的 Json_encode?

    google-apps-script - Google 表 : On checkbox click, 表中的数据应发送至所选电子邮件地址

    c# - C#中对象的碰撞检测