0

I have a jQuery ajax call with the following code:

var dataString = 'title=' + title;
alert ('datastring: ' + dataString); // This reports the correct value

$.ajax({
type: 'POST',
url: 'script.php',
data: dataString,
cache: false,
success: function(newTableID) {
    alert ('newTableID: ' + newTableID);
    // Do some stuff with the ID
},
error: function(response) {
    alert('failed: ' + response); 
            // The above displays "failed: [object Object]"
}
});

It doesn't matter what I have in my php script, I still get the same result: The alert in the error part of the ajax call displays a message box showing "failed: [object Object]". I've even tried a simple echo in the php script - I don't think the php script is running at all from this ajax call.

The url for the script is correct - the js file and the script.php file are in the same folder.

Can anyone shed some light on what I may be missing here?

Thanks.

2
  • 1
    We're gonna need to see script.php. Are you sure it's located in the same directory as your JS file? Commented Jan 15, 2013 at 23:40
  • how sure are you that it's getting reading the php script ? even if they are in the same folder, it does not necessarily mean it will read it. Put fully qualified path or an uri. Commented Jan 15, 2013 at 23:44

3 Answers 3

1

If you receive a failed, it's because your script.php is not available. You can try to use console.log(response) instead of alert() and you should see what is into your variable with Firefox or Chrome debugging tools.

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

1 Comment

This was the issue. When I changed the url in the ajax call to scripts/script.php it worked. I'm assuming that's because the path is supposed to be relative to the file the script is running on, and not the file the js script code is actually in... Thanks everyone else for your comments/attempts at help, too!
0

The error callback is being passed an object, but alert() can only show strings. Apart from some magic cases, turning an object into a string in JS gives you '[Object object]'.

Try using console.log instead of alert, which will show you the structure of the object in your browser's debug console (usually summoned with F12)

Also in the console (or if you install Firebug) will be a panel showing the network requests and responses, which may give you an idea of why there is an error.

Comments

0

You have to place : instead of =

var dataString = 'title:' + title;

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.