vambora
vambora

Reputation: 3

JQuery success callback isn't being called or?

I have searched a lot of threads but i can't find any answer that suits me. Now I will try to explain my problem.

I have a simple jQuery script that does a little ajax request. its simple like this:

print("
<script>
    function buscaCaracteristicas(idTotalizador){  
        var target = '../../ajax/doSomeSearch.php';
        $.ajax({
            url: target,                 
            //dataType: 'html',
            dataType: 'text',
            type: 'POST',
            success: function(msg){
                alert(msg);
            }
        });
    }
</script>
");

And the PHP page does this:

<?php
    $ret = "<p>hello world</p>";
    exit($ret);
?>

I have tried putting the return data on an HTML element via $.html(msg) too. Problem is: alert or the $.html() on callback NEVER fires although on firebug I can check the request has the return code 200 OK.

Notice that the function code is wrapped into a PHP print("") command and the return dataType is HTML which is what I really need to get (can't use JSON).

Any miraculous advice? Thanks in advance.

Solved: changing dataType to 'text' according to Splash-X advices it worked.

Upvotes: 0

Views: 215

Answers (3)

Snake Eyes
Snake Eyes

Reputation: 16764

try this way :

<?php
    $ret = "<p>hello world</p>";
    echo $ret;
?>

and see the result

UPDATED code:

<?php
        if(isset($_POST['value_s']))
        {
          $ret = "<p>hello world</p>";
          echo $ret;
        }
        else {
        echo "Nothing received !";
       }
?>

and the javascript:

$.ajax({
            url: target,                 
            //dataType: 'html',
            data: { "value_s":true},   //or something different value for `value_s`
            dataType: 'text',
            type: 'POST',
            success: function(msg){
                alert(msg);
            }
        });

Upvotes: 1

Nick Bork
Nick Bork

Reputation: 4841

We've tracked the issue down to the dataType attribute on the AJAX request:

 print(" 
 <script> 
     function buscaCaracteristicas(idTotalizador){   
         var target = '../../ajax/doSomeSearch.php'; 
         $.ajax({ 
             url: target,                  
             dataType: 'html' ,
             type: 'POST', 
             success: function(msg){ 
                 alert(msg); 
             } 
         }); 
     } 
 </script> 
 "); 

Our suggestion was to add an error handeler and change the dataType:

      $.ajax({ 
             url: target,                  
             dataType: 'html' ,
             type: 'POST', 
             success: function(msg){ 
                 alert(msg); 
             },
             error: function(){
                  alert('error occured');
             }
         }); 

After getting the error alert we suggested removing the dataType property and letting jQuery auto-determine it OR changing it to text. The OP decided to specify the type as text and it worked:

      $.ajax({ 
             url: target,                  
             dataType: 'text',
             type: 'POST', 
             success: function(msg){ 
                 alert(msg); 
             }
         }); 

The issue was resolved

Upvotes: 0

sadmicrowave
sadmicrowave

Reputation: 40942

You need your php page to return the value of $ret to your ajax call; try the following:

<?php
    $ret = "<p>hello world</p>";
    return $ret;
?>

Upvotes: 0

Related Questions