简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM