Reputation: 1
When I debug is code in Firebug, it works fine - it inserts records in my database and redirects to a URL. But it doesn't work. It gives me an error, alert('error')
, when I don't use a debugger. Why? val
is a serialized array,
$('#target').submit(function() {
if (validation == true) {
$.ajax({
type: "POST",
url: "some url",
dataType: "jsonp",
data: val,
success: function(data, textStatus) {
alert('success');
},
error: function(data) {
alert("error");
}
});
}
else {
return false;
}
});
//PHP Code
$return['url']= "/index.php?action=jobSeeker/jobSeekerRegistrationConfirmation";
echo $_GET['callback']."(".json_encode($return).");";
Upvotes: 0
Views: 268
Reputation: 9330
I think your problem is this,
You are making the POST
request with ajax
, but in php
echo $_GET['callback']."(".json_encode($return).");";
looking for a $_GET
, hence the response would not be a valid jsonp
formatted one. Though the execution will update the database. This is the reason to get the error.
The redirection will also be successful, since it is not having any relationship with ajax success
.
Upvotes: 0
Reputation: 5770
You said when this behaves correctly, you direct the user to a new URL. If that's the case, could this be an issue of your form submitting and redirecting to a new page before your JS success call has a chance to execute?
If you change
$('#target').submit(function() {
to
$('#target').submit(function(e) {
e.preventDefault();
your $.ajax success callback should run, then you will need to continue the URL redirect with the help of JS.
Upvotes: 2