2

Background:

  • Using jQuery 1.7 client side
  • PHP server side
  • Using json responses with json_encode php function
  • The content-type header is correct, any of these works: text/plain,text/x-json,application/json.
  • There's no errors thrown from my php code
  • Am working on Firefox 11
  • Am using the js console and the other web's developer tools
  • HTTP/1.1 200 OK

In this Javascript code, the success event is never fired:

$.ajaxSetup({cache:false,
        success:function(d) {
            console.log("ok from setup with data "+d.toSource())
        },
        complete:function(xhr,ts){
            console.log("Ajax finished reponse:"+xhr.responseText)
        },
        error:function(jqXHR, textStatus, errorThrown){
            console.log("Error")
        }
});
$.getJSON("test2.php",{},function(data){
    //some code here
});

When I do it this way, it works:

$.ajaxSetup({cache:false,                
        complete:function(xhr,ts){
            console.log("Ajax completado con:"+xhr.responseText)
        },
        error:function(jqXHR, textStatus, errorThrown){
            console.log("Error")
        }
});
$.getJSON("test2.php",{},
     function(data){
            //some code here
     }).success(function(d){
            console.log("success applied directly, data "+d.toSource())
        }
);

In both cases the complete event is always fired and the error one never. However, in the second code the success is fired. Obviously for .get() method it's the same.

PHP code:

<?php header("Content-Type:application/json;charset=utf-8");
    //or whatever text/x-json text/plain, even text/html, jquery do the dirty job
echo json_encode(array("stat"=>"1")) ?>

My objectives:

  1. I want to fire the same success event to all ajax requests
  2. I want to use the json data returned by the request in my success event, and if it is possible, without convert the raw jqXHR responseText to a json again

The problem is strange, any ideas?


I read all these questions:

And I'm pretty sure none of them are my problem.

Community
  • 1
  • 1
mark
  • 23
  • 1
  • 5

1 Answers1

5

Take a look at the ajaxSetup documentation: http://api.jquery.com/jQuery.ajaxSetup/

Note: Global callback functions should be set with their respective global Ajax event handler methods—.ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend()—rather than within the options object for $.ajaxSetup().

I think that's your problem right there.

UPDATE

If you want your ajax handlers to be global for any ajax request on the page, do something like this:

$(document).ajaxSuccess(function(d){
    console.log("ok from setup with data "+d.toSource());
});
Dan A.
  • 2,914
  • 18
  • 23
  • ok i get that, but that i really want is to fire the same success event to all ajax request to do some stuff, but without add ajaxSuccess() to each object. Thanks for your answer. – mark Apr 03 '12 at 22:35
  • Try this: `$(document).ajaxSuccess(...)` – Dan A. Apr 03 '12 at 22:49
  • Also, what browser are you using? `.toSource` is Gecko-only. – Dan A. Apr 03 '12 at 23:01
  • yes that's can be a solution, but there's a problem the i dont especify very well my needs, i have to do some stuff with the json object returned – mark Apr 03 '12 at 23:08
  • I'm assuming that my solution works based on the example in your question. Is that true? If there's more to it than that, you'll need to update your question. – Dan A. Apr 03 '12 at 23:24
  • The ajaxSuccess() recieves (event,xhr,options) and the success recieves (**data**,textStatus,xhr) and for my needs i need that data y i dont want to convert _again_ the raw responseText to a json object if jquery do it once. But am not going to get strict about it. Thanks for your solution. By the way my browser it's firefox 11, and, yes the .toSource function is not standard – mark Apr 03 '12 at 23:25
  • Ah, I see what you mean now. I guess I haven't dealt with ajax global handlers enough to encounter that. Here's a link that could probably help with that: http://www.pixelastic.com/blog/70:missing-data-in-jquery-ajax-event-ajaxsuccess – Dan A. Apr 03 '12 at 23:38
  • 2
    Excellent Dan! I found a comment by Ben Kojabash in that web site and he's totally right the .ajaxSuccess actually recieves as 4th param the data, but that's not documented in the official jquery API. There's no need to use $.httpData() or $.parseJSON(). Thanks – mark Apr 03 '12 at 23:56
  • Very cool. :) I'll upvote any comment that confirms an undocumented feature of jQuery. – Dan A. Apr 04 '12 at 00:20