0

I am running a wamp server from which I am developing a webpage. 4 months ago, everything was working. However, now when I run the page, the XMLHttpRequestObject.send("data=" + data); does not send the data.

Running the chrome debugger, I got the message:

Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource. 
Failed to load: 'file:///C:/wamp/www/hello.php'.

Going through this post: XMLHttpRequest cannot load file. Cross origin requests are only supported for HTTP

a solution would be to run a local server. However, is there a way of doing this by only using WAMP?

Community
  • 1
  • 1
user2883071
  • 960
  • 1
  • 20
  • 50

1 Answers1

2

The error message is quite explicit about what you're doing wrong--you need to specify a URL to send the request to that uses one of the mentioned protocols:

var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost/hello.php', true);
xhr.send('data=' + data);

You can't point to C:\wamp\www\hello.php, because that's not where Apache is serving it from. If you're using the default configuration, you probably want to use the URL http://localhost/hello.php.

As for running a local server, that's what the 'A' in 'WAMP' is. Apache is a web server, which you can use to serve scripts and resources, e.g. a PHP script that is able to parse the data that you POSTed from your XHR request.

It's also worth mentioning that simply sending strings is not always the best way to send data to a server. Depending on what you're sending, you might consider using the FormData or Blob objects in the xhr.send() call.

mpowered
  • 13,162
  • 2
  • 15
  • 18
  • Yes, this worked. Thank you very much. Earlier I was pointing it to C:\wamp\www\hello.php and it worked. But I guess chrome had a security update and this changed. Thanks! – user2883071 Dec 24 '14 at 19:30