0

OK, guys, thanks for your time, before anything. Here is the thing, I am learning jQuery and I am stuck in the AJAX chapter, because this next thing is not working, and I have been searching a lot. Well, I have a PHP page in my PC (using WAMP Server), with the next simple code:

<?php   
    echo '<p>Hola</p>';
?>

The file's name is 'saludo.php' and it's inside 'pruebaAJAX' folder, in 'www'. Now I have my jQuery code, also really easy:

$(document).ready(function(){

    $.get('http://localhost/pruebaAJAX/saludo.php', function(data){
            $('#contenido').html(data);
    });
});

Where 'contenido' is a DIV in the HTML page. If I access 'http://localhost/pruebaAJAX/saludo.php' using a link form my HTML page, everything works amazing, but somehow with $.get, things don't work. Every example I have checked is really close to this. Why then my concrete example is not running well? (actually not running at all). Could someone please tell me what is wrong in this code? Thank you very much...

Alan Espinet
  • 114
  • 6
  • 1
    Any errors in the browser console? (Hit F12) – Victor Levin Jan 26 '16 at 17:13
  • Alan, if you think asking this in spanish would help, please move your question to: http://es.stackoverflow.com/ – AGE Jan 26 '16 at 17:15
  • Try adding `textStatus` as second parameter at `$.get()` callback function, including `console.log(textStatus)` within body of callback function. Also try substituting `.done()` , `.fail()` for single callback function `$.get().done(function(data, textStatus) {console.log(textStatus)}).fail(function(jqxhr, textStatus) {console.log(textStatus)})` – guest271314 Jan 26 '16 at 17:17

1 Answers1

0

If you have this kind of error in js console

'...' has been blocked from loading by Cross-Origin Resource Sharing policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin '...' is therefore not allowed access. The response had HTTP status code 404.

This is probably CORS problem. You can read more about this problem here: How to get a cross-origin resource sharing (CORS) post request working

You can always prototype Your code on localhost in browser with turned off CORS policy. For example this plugin might work: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

Community
  • 1
  • 1
Everettss
  • 15,475
  • 9
  • 72
  • 98
  • Well, I have the next issue in my console window: XMLHttpRequest cannot load file:///C:/wamp/www/pruebaAJAX/saludo.php. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. – Alan Espinet Jan 26 '16 at 17:59
  • @AlanEspinet Looking at your error message it seams that you have problem with configuration WAMP. – Everettss Jan 26 '16 at 18:37