Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Post blob object gives Uncaught TypeError: Illegal invocation

I am trying to post a file using jQuery post, and get TypeError: Illegal invocation

function fbUpload(token){
  var dataURL = canvas.toDataURL('image/jpeg', 1.0)
  var blob = dataURItoBlob(dataURL)

  $.post( "https://graph.facebook.com/me/photos", {
    'access_token': token,
    'source': blob
  }, function( data ) {
    console.log(data);
  });
}

Could it be because of the 'blob'?

This is working fine:

function fbUpload(token){
  var dataURL = canvas.toDataURL('image/jpeg', 1.0)
  var blob = dataURItoBlob(dataURL)
  var formData = new FormData()
  formData.append('access_token', token)
  formData.append('source', blob)

  var xhr = new XMLHttpRequest();
  xhr.open( 'POST', 'https://graph.facebook.com/me/photos', true )
  xhr.onload = xhr.onerror = function() {
    console.log( xhr.responseText )
  };
  xhr.send( formData )
}
like image 758
ilyo Avatar asked Mar 08 '16 16:03

ilyo


1 Answers

You need to use $.ajax and provide the FormData object to the data parameter. $.post only takes data as a string or as a plain object. Try this:

function fbUpload(token){
    var dataURL = canvas.toDataURL('image/jpeg', 1.0)
    var blob = dataURItoBlob(dataURL)
    var formData = new FormData()
    formData.append('access_token', token)
    formData.append('source', blob)

    $.ajax({
        url: "https://graph.facebook.com/me/photos", 
        type: 'POST',
        data: formData,
        processData: false,
        contentType: false,
        success: function(data) {
            console.log(data);
        }
    });
}
like image 89
Rory McCrossan Avatar answered Oct 20 '22 00:10

Rory McCrossan