0

I'm trying a really simple test just trying to get some AJAX working. So I've got a PHP file which just echos something if the POST variable 'test' is set.

When I run the script, it alerts the errors but there's no error message just "error" and then the next alert is blank.

How can I get this working?

PHP

if (isset($_POST['test'])){
    echo "Some data";
}

AJAX:

url = "http://***/test.php";  
$.ajax({  
                 url: url,  
                 data: { test: "test" },  
                 type: "POST",  
                 crossDomain: true,  
                 dataType: 'html',  


                 success: function (data) {  
                 alert ("Successful");
                 alert (data);

                 },  
                 error: function (xhr, err) {  
                     alert (err); 
                     alert(xhr.responseText);  
                 }  
             });
dlofrodloh
  • 1,728
  • 3
  • 23
  • 44
  • 1
    Look at your browser's developer tools. Look at the JavaScript console. Does it report any errors? Look at the Net tab. Is the request being made? Does it get a response? Do they contain the data you expect? – Quentin Nov 12 '14 at 21:28
  • 2
    `crossDomain: true` probably doesn't do what you imagine it does. – Quentin Nov 12 '14 at 21:29
  • 2
    `data: { test: "test" }`, what you have `data: test="test"`isn't valid JSON, unless SO is dropping quotes – stakolee Nov 12 '14 at 21:30
  • 1
    @stakolee — JavaScript object literals are not JSON. That is probably the problem here though. While it is fine to encode data manually and set `data` to a string, `test="test"` isn't a properly encoded string. – Quentin Nov 12 '14 at 21:31
  • Changed it to { test: "test" } (edited question).. same thing though, no change in output – dlofrodloh Nov 12 '14 at 21:32
  • What is the output of `print file_get_contents("php://input");` put it outside of the 'if' conditional – stakolee Nov 12 '14 at 21:42
  • I put print file_get_contents("php://input"); outside the if in the php script and ran the script and there's nothing, just a blank page. – dlofrodloh Nov 12 '14 at 21:45
  • 1
    How are you looking at the page returned from the script? Are you using the browser's developer tools as I said you should in my first comment? – Quentin Nov 12 '14 at 21:46
  • I'm using Firefox, I've never used the developer tools before. I activated the NET tool from the Web Developer menu and it shows it has a GET request when I refresh the php page and a response time of 148ms – dlofrodloh Nov 12 '14 at 21:50
  • The GET request sounds like the page containing the JavaScript. You should have a POST request when the ajax function is called. If you don't see it then presumably the script either isn't running or is erroring out before sending the request. Look at the console tab of the developer tools to see if there are any errors. – Quentin Nov 12 '14 at 21:53
  • No I'm pretty sure the GET request is just me loading the php script in my browser. The console is blank and then I refresh the php script in my browser and it says it had a GET request. Haven't done anything with the script... I'll check with the script now though – dlofrodloh Nov 12 '14 at 21:55
  • In the console I got this error: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://*** This can be fixed by moving the resource to the same domain or enabling CORS. – dlofrodloh Nov 12 '14 at 21:58
  • 1
    If you would have given us that information to begin with, this huge comment chain wouldn't have happened, you would have had an answer 30 min ago. – Kevin B Nov 12 '14 at 21:59
  • I've never used the console before so I didn't know I could see that error. I'm still getting this error when I move the resource onto the same domain. – dlofrodloh Nov 12 '14 at 22:01
  • I'm also getting : The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol. – dlofrodloh Nov 12 '14 at 22:03
  • It has to be more than same domain, it has to be same domain, sub domain, port, and protocol. – Kevin B Nov 12 '14 at 22:53

0 Answers0