response.statusText=“OK” but falls into error callback from ajax with jQuery

后端 未结 6 891
余生分开走
余生分开走 2021-01-13 16:16

Server side:

 ....
    $_SESSION[\'accountId\'] = $accountId;
    $_SESSION[\'type\'] = $_POST[\'accountType\'];
    echo \'1\';
    return;
<
相关标签:
6条回答
  • 2021-01-13 16:37

    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');
        }
    });
    
    0 讨论(0)
  • 2021-01-13 16:38

    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.

    0 讨论(0)
  • 2021-01-13 16:39

    did you forget:

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

    on server?

    0 讨论(0)
  • 2021-01-13 16:41

    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'); }
    
    0 讨论(0)
  • 2021-01-13 16:43

    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);
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-13 16:50

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

    $.ajax(
    {
        url:'foo.php',
        type:'POST',
        ...
    
    0 讨论(0)
提交回复
热议问题