Thiago Dorneles
Thiago Dorneles

Reputation: 21

XMLHttpRequest doesn't works like $.ajax

I'm creating a crawler and I need to execute some javascript to get its return.

In both cases below the code works, but with XMLHttpRequest it doesn't return a specific part of HTML. I need to capture the element SELECT with id "listaPartes"

With $.ajax works, and with XMLHttpRequest it doesn't.

What's difference in the codes below?

// with AJAX
var url = 'http://www.stf.jus.br/portal/processo/listarProcessoParte.asp';
var data = {
    "dropmsgoption": "4",
    "numero": "andre",
    "partesAdvogadosRadio": "4",
};

 var response = $.ajax({type: 'POST',
                      url: url,
                      data: data,
                      async: false
                    });

return response.responseText;
// with XMLHttpRequest
var xhr = new XMLHttpRequest();
xhr.open('POST', url, false);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
var formData = new FormData(); 
formData.append('dropmsgoption', '4');
formData.append('numero', 'name');
formData.append('partesAdvogadosRadio', '4');
xhr.send(data);
return xhr.responseText;

Upvotes: 2

Views: 222

Answers (1)

Mark Eriksson
Mark Eriksson

Reputation: 1475

On your penultimate line, you're doing

xhr.send(data);

When you're data variable is formData

So you should be doing

xhr.send(formData);

Upvotes: 1

Related Questions