0

I have a function for making a post request.

$('#save').click( function() {
  ...
  $.ajax({
    type: 'POST',
    url: 'logic/save.php',
    data: { 'json': JSON.stringify( post ) },
    dataType: 'jsonp',
    success: function( data ) {
      console.log('success!')
    }
  });
} 

The request handler is written in php, as you can see, and performs operations with mysql. Because of my poor knowledge of php and requests, I do not see any success! output in the console. Php code runs fine, and the mysql queries runs with no errors.

What I want is to be able to write something in php code at the end so that my code in js could receive it as a positive response (like 200, OK) and the success! line in the console will then (I believe) appears.

What code should I add to the php file?

UPDATE: since many of people requested the php code, here's the link to it. Thank you for fast replies, guys.

16
  • 1
    Try to add error handler. Commented Jul 29, 2013 at 12:46
  • 1
    @Astro There is absolutely a need in your PHP code. Examples: is your PHP responding with 200 OK? Are you sending back valid JSON? Are you sending any other output back to the client besides the JSON? If using JSONP, are you using the callback parameter? Etc. Commented Jul 29, 2013 at 12:49
  • 1
    @ColinMorelli I totally agree, I'm just saying that syntax is correct. Commented Jul 29, 2013 at 12:50
  • 1
    @SnowBlind Fair point, though those kinds of things are usually some of the first I point out when people have problems because it's hard to believe that someone would do them intentionally :). Commented Jul 29, 2013 at 12:51
  • 1
    Just in case, try add , error: function (msg) { alert("Error: " + msg); } to ensure. Commented Jul 29, 2013 at 12:56

1 Answer 1

4

First, you should switch from jsonp to json, as you have no need for jsonp in this case. The following instructions assume you have made that change.

The bug is in your PHP code when you respond back to the client. You do so with this line:

print_r( "New item inserted.\n" );

However, jQuery is expecting you to be returning JSON back from your endpoint (and, truthfully, it should, because sending back plaintext to an ajax client is almost never the right way). We can make this work with jQuery by changing the line above to something like this:

print json_encode( array('success' => true, 'message' => "New item inserted." ) );

You should find yourself in the success callback in your client. This code would work for your success handler:

{ // ...,
    success: function(response) {
        if (response.success) {
            alert(response.message);
        }
    }
}

You'll probably want to use something other than an alert, though. But that's up to you.

Sign up to request clarification or add additional context in comments.

4 Comments

"jQuery is expecting you to be returning JSON back from your endpoint" - The OP's code is expecting jsonp, not json. (I know you go on to talk about changing the dataType, but initially you're talking about what the current code is expecting.) In any case, +1.
@nnnnnn Valid point, updated the answer and now it's the first sentence
Here's what I did. I have changed jsonp to json, removed any echo and print_r inside the php code, and added there array with success line. Unfortunately, I got the alert pop up window. Then I noticed that @colin-morelli edited the post, I used the edited version and success!. Thank you guys!
@Astro Yeah, my pre-edited code didn't actually json_encode the response lol - in other words, I was doing exactly what I was trying to tell you not to be doing. But I'm glad it's working now.

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.