user3510787
user3510787

Reputation: 7

My AJAX call is not working

I have a simple JavaScript code like this:

var xhr = new XMLHttpRequest, action = "action=latest", page = "http://192.168.1.115/wp-content/themes/HSV%20Saints/setphoto.php";
    xhr.open("POST", page, true);
    xhr.setRequestHeader("Content-type", "application-x-www-formurlencoded");
    xhr.send(action);

    xhr.onload = function(){
            console.log("XHR Onload");
            console.log(xhr.responseText);
            console.log(action);
    }

I have a PHP script like this:

<?php

$action = $_POST['action'];
echo $action;
echo 'Test message';
?>

The $action variable is not showing up, but the 'Test message' is (in the console). I don't understand why it can't send var action

Upvotes: -1

Views: 68

Answers (2)

Yogesh Patel
Yogesh Patel

Reputation: 712

Try var_dump ($_POST); in php code to see what you are getting in there.

Then you will come to know what is going wrong.

Upvotes: 0

Denys Lieu
Denys Lieu

Reputation: 270

Send method of XMLHttpRequest takes data as argument only in case of POST request. Look here https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest For GET, your action should be part of URL.

Upvotes: 1

Related Questions