0

I am sending data using an XMLHttpRequest to my REST API in PHP and don't know how to get the response back. I am using echo in my REST API if everything is ok, and data was processed.

JS code

var form = new FormData();
form.append("name", "Nicholas");
form.append("photo", $scope.imgs[0].file);

// send via XHR - look ma, no headers being set!
var xhr = new XMLHttpRequest();

xhr.onload = function() {
    console.log("Upload complete.");
};
xhr.open("post", "http://yuppi.com.ua/server/rest/add.php?key=453sdfg3t&action=add", true);
xhr.send(form);
alert(xhr.responseXML);

And alert always returns NULL

PHP code:

$form = file_get_contents("php://input");

echo "Form: " .json_encode($form);
echo "Files: " .count($_FILES);
echo "POST: " .print_r($_POST);
Crackertastic
  • 4,793
  • 1
  • 26
  • 37
yu.pitomets
  • 1,204
  • 2
  • 12
  • 35
  • 1
    You need to expand more on this. What have you tried? Can you supply current code? etc etc/... – Darren Nov 04 '14 at 02:24
  • 1
    check this http://api.jquery.com/jquery.get/ – Satya Nov 04 '14 at 02:24
  • I updated question with code. I don't wanna using jquery, to avoid mixing is with angularjs. – yu.pitomets Nov 04 '14 at 02:27
  • possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Musa Nov 04 '14 at 02:31
  • If you don't want to use jQuery, use Angular's concept of "resource" to make AJAX calls. But definitely do NOT write your own XmlHttpRequest() calls. Those will have cross-browser compatibility problems and maintainability problems and are generally terrible to work with. – Alex Weinstein Nov 18 '14 at 01:19

0 Answers0