webix.ajax().post error and success

edited April 2016 in Technical questions

I have the function below.
What does the server side code need to return so that
the correct (error or success) is executed?

webix.ajax().post("URL", variableJSON, {
                    error:function(text, data, XmlHttpRequest){
                        alert("error");
                    },
                    success:function(text, data, XmlHttpRequest){
                        alert("success");
                    }
                });
Tagged:

Comments

  • Error callback will be executed if the status code of an HTTP request will be 0 or >=400.

  • Ok, i got it. This is only for "hard errors" and NOTto handle logical business logic responses(like: Transaction failed (balance low)) from server side.

  • to handle logical business logic responses

    Yep, if you are not using response code, to mark an error, you will need to check data in the success response.

    By using the promise syntax it will look like

    var data = webix.ajax().post(url, data).then(function(data){
       //return { "error":"something"} to trigger an error
    
       var is_error = data.json().error;
       if (is_error) throw(is_error);
    });
    

    data.then(success_callback, error_callback);

  • @maksim for a successful response , should'nt the example be like this?

    var data = webix.ajax().post(url, data).then(function(data){
       //return { "error":"something"} to trigger an error
    
       var is_error = data.json().error;
       if (is_error) throw(is_error);
       else return data; // return data to trigger success_callback
    });
    data.then(success_callback, error_callback);
    
  • Yep, sure, I have shown an error handling, but forgot to write the last return line

  • Can you plesae put an example how can I get fields from data in success response? I am trying data.json().fieldname, but it is always undefined. Thanks

  • edited September 27

    Hey @ftedin, please take a look at the following example: https://snippet.webix.com/ekthk202. I'm not sure what the issue in your case might be, but you are either not getting a response object, or you are referring to the wrong property name.

  • The sample you sent is a get, I am talking about post request. I am able to get the json back and I see the correct values in the console, but I am not able to get the value of each field in the json, I just see them when I print it out in console using data,json(), but I am not able to reference any field on it. Thanks.

  • The principle stays exactly the same, be it a GET or a POST request - https://snippet.webix.com/0lws9r0y. I am not quite sure what the issue might be in your case, but it is probably related to your server specifically.

  • JayJay
    edited October 9

    how to set up the cross origin headers for the post call ? please share some thoughts on it

Sign In or Register to comment.