[IGNORE] url to send a POST request with AJAX

php

#1

Hi
I’m testing AJAX with a simple XMLHTTP request but no luck

script

// data variable supplied elsewhere

url = ‘scribbles.php’;

var r = new XMLHttpRequest();
r.onreadystatechange=function(){};

r.open(‘POST’, url, true);
r.setRequestHeader(‘Content-type’,‘application/json’);
r.send(JSON.stringify(data));

on scribbles.php
all I’m doing is var_dump($_POST);

but no luck, I get an empty array

both the script and the php page are on my workspace.

Is it a url problem?
tried various ways e.g ide.c9.io/myUserName/myWorkspace/scribbles.php

Regards


#2

When making requests with Ajax I simply entered the path of the php file. Have you tried localhost/filename.php?


#3

Within the IDE, navigate to the folder that has “scribbles.php”, right click on “scribbles.php” and choose “open terminal here”. Then, within the terminal, type “pwd” and hit enter. That is the command for “Print Working Directory”. That will give you the exact path of scribbles.php that you can then use in your script.


#4

Oh my word I’m awfully sorry
it turns out the problem was the way I sent the data.

For everyone’s benefit the best answer on how to POST and retrieve data in JSON format
to a php server is at stack overflow here:

AJAX: JSON to PHP and back again, no jQuery


#5

thanks very much everyone for replying!

the url was fine as it was scribbles.php
the problem was the data format being sent over AJAX!
See above

ta