0

查看 jquery $.post 文档,它似乎没有显示如何使用它设置请求标头。谷歌搜索我认为向 POST 方法添加请求标头的唯一方法是简单地使用 $.ajax。但我在转换代码时遇到问题。

这是 $post 代码

$.post(url_ajax_signature, signature_params, function(response) {
  //send through crossdomain page
  var windowFrame = document.getElementById('postMessageFrame').contentWindow ;
  var data = {
          params: response.params,
          url: response.url,
          content: dataURL
  }

  //send data of s3 request signature and base64 binary data
  windowFrame.postMessage(data, 'http://<?=$url_iframe_host?>');                
}, 'json');

非常感激。

4

3 回答 3

3

需要像这样的语法来修复它:

$.ajax({
            type: "POST",
            url: url_ajax_signature,
            data: JSON2.stringify({ signature_params }),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                var windowFrame = document.getElementById('postMessageFrame').contentWindow;
                var data = {
                          params: msg.params,
                          url: msg.url,
                          content: msg.dataURL
                  }
                //send data of s3 request signature and base64 binary data
                windowFrame.postMessage(data, 'http://<?=$url_iframe_host?>'); 
            }
        });
于 2013-10-16T03:55:26.650 回答
0

尝试

$.ajax({
    url: url_ajax_signature,
    data: signature_params,
    dataType: 'json',
    headers: {
        header: value
    }
}).done(function (response) {
    //send through crossdomain page
    var windowFrame = document.getElementById('postMessageFrame').contentWindow;
    var data = {
        params: response.params,
        url: response.url,
        content: dataURL
    }

    //send data of s3 request signature and base64 binary data
    windowFrame.postMessage(data, 'http://<?=$url_iframe_host?>');
})
于 2013-10-16T03:55:02.180 回答
0

如果要使用$.ajax()设置请求标头,请执行以下操作:

$.ajax({
    url: url_ajax_signature,
    data: signature_params,
    dataType: 'json',
    beforeSend: function (request) {
        request.setRequestHeader('your header here');
    },
    success: function(response) {
        var windowFrame = document.getElementById('postMessageFrame').contentWindow ;
        var data = {
          params: response.params,
          url: response.url,
          content: dataURL
       }

        //send data of s3 request signature and base64 binary data
        windowFrame.postMessage(data, 'http://<?=$url_iframe_host?>');
    }
});
于 2013-10-16T03:57:26.010 回答