how to set HTTP header variables using ajax call

Discussion and feedback on Construct 2

Post » Mon Feb 10, 2014 8:35 am

hi,

I like to set header variables for my ajax call rather than using POST or GET variable.

Is there a way to do that???

Note:- i have a rest API that requires me to send HTTP header variables like public key etc. So it is necessary for me to set HTTP header variables
B
9
S
2
Posts: 108
Reputation: 1,010

Post » Mon Feb 10, 2014 9:44 am

Most of http related calls to apis are processed by means of passing variables in the url parameters.

POST
url = "http://location.com/file.php"
data = "id=12345&key=6789"

GET
url = "http://location.com/file.php?id=12345&key=6789"



If you really require custom set headers, you will need to do some custom javascripting through the browser object and use/refer XmlHttpRequest in your coding.
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,567

Post » Mon Feb 10, 2014 1:13 pm

is there any plugin available for this type of work???? I am amaze that this is not provided by construct2....suppose if i want to do that then what should be starting point do i need to create new plugin to code javascript?
B
9
S
2
Posts: 108
Reputation: 1,010

Post » Mon Feb 10, 2014 1:27 pm

This is probably something that is not implemented yet.
Meanwhile, you have two options:

Either modify the ajax plugin and add options for modifying headers (shouldn't be that hard to do, but requires you to learn a bit of the SDK)

Or set up a server-side request proxy, that takes your request, modifies the headers, and forwards it to the destination - you'll need to know a server-side language (such as PHP) for that.

I recommend the first option.Fimbul2014-02-10 13:27:52
B
36
S
8
G
8
Posts: 532
Reputation: 6,903

Post » Tue Feb 11, 2014 8:02 pm

@Fimbul based on your answer i have made my own custom ajax plugin but strangely it does not work when i use header variables. It is not sending the server variables for some reasons and after spending more than 4 hours i am unable to figure out why...

But when i tried a simple html page with javascript code and it worked... Both codes are identical but not sure why in C2 prototype object it doesnt work at all...
[code]if(method_ === "POST" && data_ && hdata_)
               {
                    
                    if (request["setRequestHeader"])
                    {
                         request["setRequestHeader"]("Content-Type", "application/x-www-form-urlencoded");
                         request["setRequestHeader"]("X-UserMail", "[email protected]");
                         request["setRequestHeader"]("X-Number", "hdj2wds2323d723");
                         request["setRequestHeader"]("X-Token", "80e68a46796cf0085afc45067b83273043bb685e864ec99058b609d29f34523e");
                         
                         alert("HDPOST "+request["setRequestHeader"]);
                         
                    }
     
                    request.send(data_);
               }

[/code]
B
9
S
2
Posts: 108
Reputation: 1,010


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 32 guests