javascript - Confusion with returning values from a synchronous ajax call -


i'm having issues returning value synchronous ajax call. value want return class created server response.

here's ajax code:

function webrequest(file, data) {     return $.ajax({         url: "http://xxx.xx.xx.xxxx/xxxxx/"+file,         type: "post",         data: data,         asynch: false,         error: function(jqxhr, textstatus, errorthrown){             return new serverresponse(false, errorthrown);         },         success: function(data, textstatus,  jqxhr){             return new serverresponse(true, data);         },         timeout: 7500     });     } 

here's serverresponse.js

var success = false; var text = null;  var serverresponse = function(success, text) {     this.success = success;     this.text = text || null; };  serverresponse.prototype.issuccessful = function() {     return this.success;   };  serverresponse.prototype.getdata = function() {     return this.text; }; 

the returned value of webrequest(..) follows:

object {readystate: 1, getresponseheader: function, getallresponseheaders: function, setrequestheader: function, overridemimetype: function…}abort: function ( statustext ) {always: function () {complete: function () {done: function () {error: function () {fail: function () {getallresponseheaders: function () {getresponseheader: function ( key ) {overridemimetype: function ( type ) {pipe: function ( /* fndone, fnfail, fnprogress */ ) {progress: function () {promise: function ( obj ) {readystate: 0responsetext: ""setrequestheader: function ( name, value ) {state: function () {status: 0statuscode: function ( map ) {statustext: "error"success: function () {then: function ( /* fndone, fnfail, fnprogress */ ) {__proto__: object vm2324 controllers.js:48 

how can return serverresponse instance created within ajax call?

@fuyushimoya's answer there, return newly instantiated server response object wrapper function.

function webrequest(file, data) {     var serverresponse;     $.ajax({         url: "http://xxx.xx.xx.xxxx/xxxxx/"+file,         type: "post",         data: data,         async: false,         error: function(jqxhr, textstatus, errorthrown){             serverresponse = new serverresponse(false, errorthrown);         },         success: function(data, textstatus,  jqxhr){             serverresponse = new serverresponse(true, data);         },         timeout: 7500     });     return serverresponse; } 

this way can do

var sr = webrequest('some/file', somedata); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -