Matt Hannam Matt Hannam - 6 months ago 34
Ajax Question

How to send an Ajax POST request

I'm using this code to send a POST to my node API to generate a PDF, my node console gives me these errors:

$('#renderPDF').click(function(){
var request = $.ajax({
type: "POST",
url: 'http://localhost:8080/api/v1/generate',
data: {doc:"<h1>test 123</h1>"},
});
request.done(function(data){
console.log(data);
});
});



info: TypeError: null is not an object (evaluating 'child.transform')
info: info: phantomjs://code/shim.js:157 in transform info:

phantomjs://code/shim.js:138 in read


When I send a POST (with the Postman tool), the PDF is rendered and everything works.

localhost:8080/api/v1/generate?doc=<h1>testing 123</h1>


What am I doing wrong on my POST request?

Answer

Probably you need/can achieve it easier with formData

e.g.

data = new FormData();
data.append( 'doc', "<h1>test 123</h1>" );

$.ajax({
    url: 'http://localhost:8080/api/v1/generate',
    data: data,
    processData: false,
    type: 'POST',
    success: function ( data ) {
        alert( data );
    }
});
Comments