1

im having a problem trying to get an ajax call to trigger a php function and then return successfully. As far as i can see my syntax is correct.

But all i get back is failed! Any ideas?

EDIT

I have changed my AJAX request to send without using data, just to rule out that being a problem and i have implemented some of the things people suggested below but to no avail, heres what my 2 files look like now:

ship.js:

function set_ship()
{
//var datastring = {'ship':'true'};
$.ajax({
    type:'POST',
    url:'soap/classes/class.ship.php',
    success: function(success){
        alert('success');
    },
    error: function(fail){
        console.log(fail);
    }
});
}

And my PHP class.ship.php:

<?php
var_dump("hello");

header('Content-type: application/json');

echo json_encode(array("result"=>"true"));

From the var_dump on my PHP script i can see that the class.ship.php isnt even being called for some reason.

Thanks

5
  • If using Chrome you can check the network tab in inspector. You might see a 500 from your script. Clicking that might give you further info depending on your error reporting. Commented Nov 4, 2013 at 22:10
  • Not an answer to your question, but you're matching a boolean with a string in your code example, PHP swallows it but it looks silly. Commented Nov 4, 2013 at 22:24
  • I ran your code, and it worked for me. Is there anything else in class.ship.php besides the pasted code that could be messing up the response? Commented Nov 4, 2013 at 22:25
  • @Dan What was the URL you used? i have a sneaky suspicion that thats what is causing the error Commented Nov 5, 2013 at 8:14
  • I have /test.html /ship.js and /soap/classes/class.ship.php. Your latest edited code works, as long as I comment out the var_dump line. Stupid question, but you're including jquery in your version of test.html, right? Commented Nov 7, 2013 at 14:45

4 Answers 4

2

Please try this

 json_encode(array("result"=>"true"));

because

 json_encode(true) // will return just "true" which is not a valid json

Also try serializing the dataString , by doing

data: datastring.serialize();
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried these now, but still no luck still returning error :/
Have you tried accessing the soap/classes/class.ship.php from a browser ? are you getting all valid outputs ?
Also i believe that you have tried with he complete url, rather than relative path, as the relative path may vary depending on the page you are at ? Also try the error method with this signature, error:function(jqXHR jqXHR, String textStatus, String errorThrown ){ console.log(testStatus); }
0
  1. lowercase JSON_ENCODE
  2. success: function(success), error: function(fail)
  3. check what is returned in network tab of firebug.

Comments

0

You need to set the content header to json header('Content-type: application/json'); and make sure you request return only json coz ajax is waiting only for "JSON" and it will throw parse error

if(isset($_POST['ship']) && $_POST['ship'] == "true"){
  $result = "result";
  header('Content-type: application/json');
  echo json_encode(true);
}

Comments

0

I would suggest that you check what it being actually returned by the server.

The error callback receives one argument representing the xhr object, so you can inspect that directly by placing a breakpoint or using console logging, like this

error: function(xhr) {
    console.log(xhr);
}

Likewise, the success callback receives three parameters: the status, the returned data and the XMLHTTPRequest Object, so you can check those in the very same way:

success: function(status, data, xhr) {
    console.log(status, data, xhr);
}

You should look for the response status code and the response text in the xhr object to understand what is going wrong. If you're seeing a 200 OK response status, the data returned from the server is probably not being interpreted correctly as JSON data, so you should try setting the response header server side to application/json.

An error might occur also if something else is appended or prepended to your response. This happens especially when warnings occur in the code before returning and you have error reporting set to ON.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.