AJAX with PUT and Headers and Headers Returns

For developers using the Construct 2 Javascript SDK

Post » Tue Apr 23, 2013 1:03 pm

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.

[code]
Edittime.js
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");


Runtime.js
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);
};

[/code]

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 :(labithiotis2013-04-23 13:03:25
B
24
S
11
G
5
Posts: 140
Reputation: 5,004

Post » Wed Apr 24, 2013 2:18 pm

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

     [code]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();
                                   }
                                   
                         });     
                    }
               }

[/code]
B
24
S
11
G
5
Posts: 140
Reputation: 5,004


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests