AJAX with PUT and Headers and Headers Returns

  • Hi,

    I need an ajax plugin to handle using PUT method and to submit a GET request with a custom header and get the response header too.

    I have copied the oringal ajax plugin and got the PUT action, but i am trying to add a GET request with custom headers & response. I am struggling to get that to work.

    [b]Edittime.js[/b]
    AddStringParam("Tag", "A tag, which can be anything you like, to distinguish between different AJAX requests.", "\"\"");
    AddStringParam("URL", "The URL to put to.  Note: most browsers prevent cross-domain posts.", "\"http://\"");
    AddStringParam("Data", "The data to put, in query string form.  Be sure to URLEncode any user-entered data.");
    AddAction(3, 0, "Put to URL", "AJAX", "PUT <b>{2}</b> to URL <b>{1}</b> (tag <i>{0}</i>)", "Make a PUT request to a URL.", "Put");
    
    AddStringParam("Tag", "A tag, which can be anything you like, to distinguish between different AJAX requests.", "\"\"");
    AddStringParam("URL", "The URL to put to.  Note: most browsers prevent cross-domain posts.", "\"http://\"");
    AddStringParam("HeaderKey", "Header Key");
    AddStringParam("HeaderValue", "Header Value");
    AddAction(4, 0, "Request URL with Header", "AJAX", "Request <b>{2}</b> to URL <b>{1}</b> (tag <i>{0}</i>) with header: <i>{3}</i> with <i>{4}</i>", "Request URL with Header.", "RequestHeader");
    
    AddExpression(2, ef_return_string, "Get Header Responce", "AJAX", "HeaderResponce", "Get the header responce");
    
    [b]Runtime.js[/b]
    Acts.prototype.Put = function (tag_, url_, data_)
     {
         this.doRequest(tag_, url_, "PUT", data_);
     };
    
    Acts.prototype.RequestHeader = function (tag_, url_, headerkey_, headervalue_)
     {
         this.doRequest(tag_, url_, "GET", headerkey_, headervalue_);
     };
    
    Exps.prototype.HeaderResponce = function (ret)
     {
         var requests;
              
         this.xhr = sinon.useFakeXMLHttpRequest();
         this.xhr.onCreate = function(xhr) {
           ?requests.push(xhr);
         }
              
         ret.set_string(requests);
     };
    
    

    But when i added either of these the normal ajax call action stops working.

    Any help would be great.

    Need to have something working by end of day :(

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I have tried to make a plugin with Jquery but not making much progress. I have the following if anyone can help that would be awesome.

    https://dl.dropboxusercontent.com/u/85804468/jajax.zip

         

    instanceProto.doRequest = function (tag_, url_, method_, data_, headerkey_, headervalue_)
         {
    // Create a context object with the tag name and a reference back to this
              var self = this;
              var requestAjax;
              
              var errorFunc = function () {
                        self.curTag = tag_;
                        self.runtime.trigger(cr.plugins_.JAJAX.prototype.cnds.OnError, self);
                   };
              
              try {
                   if ( method_ === "POST" && data_ ) {
                    requestAjax = $.AJAX({
                                  beforeSend: function(xhr){
                                            xhr.setRequestHeader('auth', 'hash');
                                            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                                            xhr.setRequestHeader("Content-Length", data_.length);
                                       },
                                  type: method_,
                                  url: url_,
                                  data: data_ ,
                            contentType: 'application/x-www-form-urlencoded',
    
                            success: function (data) {
                                            self.HeaderResponce = xhr.getResponseHeader( headerkey_ );
                                    self.lastData = data.replace(/\r\n/g, "\n");
                                            self.runtime.trigger(cr.plugins_.JAJAX.prototype.cnds.OnComplete, self);
                                       },
                                  error: function() {
                                            errorFunc();
                                       }
                                       
                             });     
                        }
                   if ( method_ === "GET" ) {
                    requestAjax = $.AJAX({
                                  beforeSend: function(xhr){
                                            xhr.setRequestHeader('auth', 'hash');
                                            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                                            xhr.setRequestHeader("Content-Length", data_.length);
                                            xhr.setRequestHeader(headerkey_, headervalue_);
                                       },
                                  type: method_,
                                  url: url_,
                                  success: function (data) {
                                            self.HeaderResponce = xhr.getResponseHeader( headerkey_ );
                                            self.lastData = data.replace(/\r\n/g, "\n");
                                            self.runtime.trigger(cr.plugins_.JAJAX.prototype.cnds.OnComplete, self);
                                       },
                                  error: function() {
                                            errorFunc();
                                       }
                                       
                             });     
                        }
                   if ( method_ === "PUT" ) {
                    requestAjax = $.AJAX({
                                  beforeSend: function(xhr){
                                            xhr.setRequestHeader('auth', 'hash');
                                            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                                            xhr.setRequestHeader("Content-Length", data_.length);
                                            xhr.setRequestHeader(headerkey_, headervalue_);
                                       },
                                  type: method_,
                                  url: url_,
                                  success: function (data) {
                                            self.HeaderResponce = xhr.getResponseHeader( headerkey_ );
                                            self.lastData = data.replace(/\r\n/g, "\n");
                                            self.runtime.trigger(cr.plugins_.JAJAX.prototype.cnds.OnComplete, self);
                                       },
                                  error: function() {
                                            errorFunc();
                                       }
                                       
                             });     
                        }
                   }
    
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)