200个响应代码调用Ajax错误回调

时间:2018-10-27 12:59:31

标签: jquery ajax webhooks

下面给出的post方法将POST到一个Webhook,该Webhook接受四个参数-fields[message]fields[name]fields[email]fields[url]

Webhook成功运行。最初它将200响应发送回客户端。然后,它解析传递的参数并按要求执行。我还向网络挂钩的响应中添加了response-message: "{}"

每次返回的状态码为200。但是,错误回调已在Ajax调用中被调用。

// Static comments
(function ($) {
  var $comments = $('.js-comments');

  $('#comment-form').submit(function () {
    var form = this;

    $(form).addClass('disabled');
    $('#comment-form-submit').html('Loading...');

    $.ajax({
      type: $(this).attr('method'),
      url: $(this).attr('action'),
      data: $(this).serialize(),
      dataType: 'json',
      contentType: 'application/x-www-form-urlencoded',
      success: function (data) {
        $('#comment-form-submit').html('Submitted');
        $('#comment-form .js-notice').removeClass('notice--danger').addClass('notice--success');
        showAlert('<strong>Thanks for your comment!</strong> It will show on the site once it has been approved.');
      },
      error: function (err) {
        console.log(err);
        $('#comment-form-submit').html('Submit Comment');
        $('#comment-form .js-notice').removeClass('notice--success').addClass('notice--danger');
        showAlert('<strong>Sorry, there was an error with your submission.</strong> Please make sure all required fields have been completed and try again.');
        $(form).removeClass('disabled');
      }
    });

    return false;
  });

  function showAlert(message) {
    $('#comment-form .js-notice').removeClass('hidden');
    $('#comment-form .js-notice-text').html(message);
  }
})(jQuery);

这给了我一个Access-Control-Allow-Origin错误,所以我将dataType: 'json',更改为dataType: 'jsonp',

现在,传递的参数没有被解析。而且错误回调仍在被调用。 Webhook仍会向客户端返回200状态响应代码。

Webhook从YAML文件运行,如下所示。

- id: "add-comment"
  execute-command: "./add-comment.sh"
  response-headers:
    - Access-Control-Allow-Origin: "*"
  response-message: "{}"
  pass-arguments-to-command:
    - source: payload
      name: fields[message]
    - source: payload
      name: fields[name]
    - source: payload
      name: fields[email]
    - source: payload
      name: fields[url]

我在这里完全迷路了。如何获取成功回调以进行调用? (状态为200,响应主体为{},数据类型为jsonp)。

0 个答案:

没有答案