Wondering Coder
Wondering Coder

Reputation: 1702

converting $.post to $.ajax

Looking at jquery $.post docs it doesn't seem to show how to set a request header using it. Googling around I think the only way to add a request header to POST method is simply using $.ajax. But i'm having problem converting the code.

Here is the $post code

$.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');

Greatly appreciated.

Upvotes: 0

Views: 1235

Answers (3)

Milson
Milson

Reputation: 1575

Need to fix it like this syntax:

$.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?>'); 
            }
        });

Upvotes: 3

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

if you want to set request headers using $.ajax(), then do:

$.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?>');
    }
});

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Try

$.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?>');
})

Upvotes: 0

Related Questions