1. #1
    Ext User
    Join Date
    Oct 2007
    Posts
    11
    Vote Rating
    0
    Nisse is on a distinguished road

      0  

    Question How to trigger failure using Ext.Ajax.request

    How to trigger failure using Ext.Ajax.request


    I'm using Ext.Ajax.request to send a request.

    Now I'm trying to trigger the failure callback but I only get success.

    I was under the impression that success: false should trigger failure.

    Here's json return text from my jsp page. I set type to application/json.
    {success:false, errors:{"field-name":"Bad value", "field-name-1":"Key not found"}

    Does this success:false only work for form-based requests?
    For Ext.Ajax do I need to specifically set the http header code as well to 400 or something?

    Any input would be appreciated,

    Fredrik

  2. #2
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854
    Vote Rating
    4
    tryanDLS is on a distinguished road

      0  

    Default


    If you're calling Ajax.request directly, it's not looking at the success property of the response. Success or failure is determined simply by the HTTP response. If you want to further process the responseText to determine success/failure, you need to do it in the callback fn or the success handler. There, you would look at the responseText property to determine whether you meet your app criteria for success.

  3. #3
    Ext User
    Join Date
    Aug 2007
    Posts
    212
    Vote Rating
    0
    djfiii is on a distinguished road

      0  

    Default


    I always like code examples when I'm looking for the solution to a problem. This post had the same question I had, and trylan provided the answer I needed but for those who come next and would like to see the code that demonstrates his point, here ya go:

    Code:
    			Ext.Ajax.request({
    				url: 'mypage.asp',
    				method: "POST",
    				params: params,
    				success: function( r, o ){
    					responseObj = Ext.util.JSON.decode(r.responseText);
    					alert(responseObj.success);
    				},
    				failure: function( r, o ) {
    					alert( "Submit failed: " + r.responseText );
    				}
    			});
    So as long as your http request completes, you are going to end up in your success handler of the AJAX request, even if the logic contained in your server side script might still decide the submission was invalid for some reason. Within the AJAX success handler, I'm then decoding the JSON that is being output by my page to determine what I want to do. For now, let's say myapge.asp is returning the following JSON:

    Code:
    { success: false, errors: { reason: 'You are not authorized to use this application. Please contact so and so.' }}
    then the alert statement above would alert "false", since that's the value of my JSON response's success attribute. Let's say mypage.asp is returning the following JSON:

    Code:
    { success: true}
    well then my alert is "true". You can build on this to do whatever you need inside your sucess handler as follows:

    Code:
    			Ext.Ajax.request({
    				url: 'audit-update.asp',
    				method: "POST",
    				params: params,
    				success: function( r, o ){
    					responseObj = Ext.util.JSON.decode(r.responseText);
    					//alert(responseObj.success);
    					if(responseObj.success == true){ 
    						alert("you did something right and the server knows it");
    					} else {
    						alert(responseObj.errors.reason)
    					}	
    				},
    				failure: function( r, o ) {
    					alert( "Submit failed: " + r.responseText );
    				}
    			});

    this assumes the same JSON responses posted above. Hopefully this helps someone!

Thread Participants: 2