0 Favourites

how to set HTTP header variables using ajax call

  • 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

  • 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.

  • 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?

  • 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.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • 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...

    if(method_ === "POST" && data_ && hdata_)
                   {
                        
                        if (request["setRequestHeader"])
                        {
                             request["setRequestHeader"]("Content-Type", "application/x-www-form-urlencoded");
                             request["setRequestHeader"]("X-UserMail", "faeeea

    kkk.com");[/p]                          request["setRequestHeader"]("X-Number", "hdj2wds2323d723");[/p]                          request["setRequestHeader"]("X-Token", "80e68a46796cf0085afc45067b83273043bb685e864ec99058b609d29f34523e");[/p]                          [/p]                          alert("HDPOST "+request["setRequestHeader"]);[/p]                          [/p]                     }[/p]      [/p]                     request.send(data_);[/p]                }[/p] [/p]

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)