tags:

views:

601

answers:

6

Server side:

 ....
    $_SESSION['accountId'] = $accountId;
    $_SESSION['type'] = $_POST['accountType'];
    echo '1';
    return;

Client side:

$.ajax(
{
    type:'POST',
    data: $("#flowFormSubmit").serialize(),
    dataType:'text/plain',
    timeout:1000,
    success:function(response){
     if(-1 == response)
      alert('fail');
     else
      alert('succeed');
    }
});

I've tested that it stops right at 'return;' Under what conditions will success function not be called?

EDIT:

after adding error callback,it's caught,but didn't output useful information yet:

error:function(response){ alert(response);alert(response.statusText); },

It outputs only:

[object XMLHttpRequest]  
OK

Why does it fall in error callback?

+2  A: 

It might be a good idea to specify a URL option:

$.ajax(
{
    url:'foo.php',
    type:'POST',
    ...
karim79
excellent point
David Andres
astounding that he gets to the 'return'
Scott Evernden
The recipient URL is probably the same as the requesting URL, so perhaps jQuery uses a reasonable default (i.e, the current page).
David Andres
doh! I didn't notice that. hmmmm
JasonWoof
yeah, it does default to the current page
JasonWoof
@Davis Andres - you are right, it defaults to the current page.
karim79
@All: It's nice to learn something seemingly by accident or by some sense of reason.
David Andres
A: 

did you forget:

header('Content-Type:text/plain');

on server?

Scott Evernden
No,still no response.
Shore
A: 

1) go to the url in your browser (the one you're fetching with ajax)

2) set the error callback for the jquery ajax request.

JasonWoof
A: 

in your php code, use json_encode to return the response

eg:

$response = array('success'=>1);
echo json_encode($response);

then in your ajax success function it should be

if (response.success == 1) { alert('success'); }
A: 

Try omitting the return statement from server-side code. As far as your error callback is concerned, use the following generic ajax callback code and let us know what the status, responseText, and statusText properties are when this fails:

function callback(response)
{
  if (response.readyState == 4) 
  {
    if (response.status == 200) 
    {
      alert("Response Success:\n" + response.responseText);
    }
    else
    {
      alert("Response Error:\n" + response.statusText);
    }
  }
}
David Andres
Really weird,success callback sometimes get called,sometimes not.
Shore
A: 

There are more useful parameters to the error callback. Try getting the textStatus.

error: function(XHR, textStatus, errorThrown) { console.log(textStatus); }

Check what that is.

A quick guess is that you meant to use dataType: 'text' not 'text/plain'. (Check documentation)

$.ajax({
    type:'POST',
    data: $("#flowFormSubmit").serialize(),
    dataType:'text',
    timeout:1000,
    success:function(response){
        if(-1 == response)
                alert('fail');
        else
                alert('succeed');
    }
});
gnarf