0 Favourites

Ajax plugin to send header variables

  • I am making a new plugin for myself in which i need to send some extra data through the server header variables. Since the current official ajax plugin does not support this feature i have to create a new by myself.

    But the problem i am facing is , when i set the header variables then it stop posting the data and i see no header variables on my server side script at all.

    the changes what i done is i have just made a new action which takes server header variables... but for the debugging purpose i have added explicitly some header varibales in think it will reach the server...but strangely it never works through plugin. But if i write ajax code in some test html pages and if i apply same way to the header then it works fine.

    I dont understand why it is not working here is the sample of my plugin which is also available on my dropbox shared link

    dl.dropboxusercontent.com/u/104183650/ajaxhd.zip

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

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

    the hdata_ is new header data which fill with some variables but for the sake of testing i have hard coded some header data...the function always gets call and these header settings always works but when i call send then i recieve no POST data nor SERVER variable data...using same stuff with testing page with javascript works fine below is the successful test

    <script type="text/javascript">
    function fetchStatus(address) {
      var client = new XMLHttpRequest();
      client.onreadystatechange = function() {
        // in case of network errors this might not give reliable results
        if(this.readyState == this.DONE)
          returnStatus(this.status);
      }
      client.open("POST", address);
      client["setRequestHeader"]("Content-Type", "application/x-www-form-urlencoded");
      client["setRequestHeader"]("X-UserMail", "faa

    kkk.com");[/p] client["setRequestHeader"]("X-Number", "hdj2723");[/p] client["setRequestHeader"]("X-Token", "80e68a467969058b609d29f34523e");[/p]      [/p] client.send("mail=abcakaka.com");[/p] }[/p] [/p] fetchStatus("http://localhost/myrestapi");[/p] [/p] </script>[/p]

  • You could use firefox with firebug, then check the console (ctrl+shift+j) for advanced information and insight to your ajax calls.

    For instance, the error code (if any) could give a good indication whats wrong.

    It will give a clear indication whether or not it was a valid call.

    I use firebug with anything url calls related, as it gives moset possible insight to whats wrong with a call, and, if need be, check the contents down to the headers if need be :)

  • lennaert i have tried it but i dont see any error but some concurrent ajax call of ludi ..i will appreciate if you also try and tell me :(

  • edit: nvm, doing new test, unpacked wrong

  • Ok, got it to work partially.

    my calls go through to a php page after I added the cross domain support to my php file:

    header('Access-Control-Allow-Origin: *');

    edit: did you happen to write your test pages, and tested them, from the same server you are making your call too ?

    In that case it would work, as its the same domain.

    Bbut when you use previewe from construct, or from a mobile device where it is installed as an app, its form another domain.

  • lennaert thanks for your rapid replies , yes i am already using access control ... and i am right now only using construct2 run button which run the application using virtual server ...or i just use localhost/debug to debug...but on any case i do not find header variables which i send to the php page...are you able to receive those variables at php end???

  • If you use firefox, and install the firebug plugin, then have the console open as your call is made, you can see the all the details of the calls, including the headers.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Can you post an example of what to input in the HData entry field ?

  • HData entry is just dummy , because i was hardcoding the data to send header values...so in hdata just put anything for now. I have checked in firebug the strange thing is this i cannot see the post script. But since i am logging at my php end so it does come to the php page..seems like my firefox firebug is even not working when i use header value without that it works fine.

  • the reaspn why i am not using hdata because i was trying to test first using hardcode values but i was not able to succeeded even using hardcode values.

  • Content-Type     application/x-www-form-urlencoded

    X-UserMail     faakkk.com

    X-Number     hdj2723

    X-Token     80e68a46796cf0085afcb609d29f34523e

    Firebug shows this and my php code other hand is

    Yii::log("NO ERROR m = ".$_SERVER["HTTP_X_Token"]." -- ".$_SERVER["HTTP_X_UserMail"],"MyLog");
    

    above code just write the data in my log file for these varibales and they are coming as null. Previously the same service when i was calling through the web page by passing X-UserMail etc were working fine ...

  • Your using Yii framework :) nice stuff, but, does way too much in the background.

    try a blank php page and add:

    <?php

    header('Access-Control-Allow-Origin: *');

    var_dump(getallheaders());

    ?>

  • thanks for your rapid replies , yes i am using yii for my game...

    I have used the var_dump like as you said on another test page and it displays everything fine if i fetch the page using ajax by test.html javascript...

    but using with construct2 , when I send its header firebug shows it is seems fine but response tab does not display anything..it seems like for some reason whenever i set header and send through construct2 the server variables does not reach including the post variables... but shocking these header values in header and post values in post tab of firebug shows they are being sent successfully through firebug... :( still i dont get it why php code is unable to fetch these values when we use construct2 plugin?

  • Using HData like ""   (empty)

    I get this in the response tab:

    array(14) {

    ["Content-Length"]=>

    string(1) "4"

    ["Content-Type"]=>

    string(48) "application/x-www-form-urlencoded; charset=UTF-8"

    ["Accept"]=>

    string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"

    ["Accept-Language"]=>

    string(14) "en-gb,en;q=0.5"

    ["Cache-Control"]=>

    string(8) "no-cache"

    ["Connection"]=>

    string(5) "close"

    ["Dnt"]=>

    string(1) "0"

    ["Host"]=>

    string(15) "lennaerthohe.nl"

    ["Origin"]=>

    string(22) "http://localhost:50000"

    ["Pragma"]=>

    string(8) "no-cache"

    ["Referer"]=>

    string(23) "http://localhost:50000/"

    ["User-Agent"]=>

    string(72) "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0"

    ["X-Forwarded-For"]=>

    string(13) "94.212.140.34"

    ["X-Real-Ip"]=>

    string(13) "94.212.140.34"

    }

    As soon as I put something in the HData entry field t doesnt work.

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

    kkk.com");[/p]                          request["setRequestHeader"]("X-Number", "hdj2723");[/p]                          request["setRequestHeader"]("X-Token", "80e68a4679e864ec99058b609d29f34523e");[/p]                          [/p]                          [/p]                     //     alert("HDPOST "+request["setRequestHeader"]);[/p]                          [/p]                     }[/p]      [/p]                     request.send(data_);[/p]                }[/p] else if (method_ === "POST" && data_)[/p]                {[/p]                     alert("POST");[/p]                     [/p]                     if (request["setRequestHeader"])[/p]                     {[/p]                          request["setRequestHeader"]("Content-Type", "application/x-www-form-urlencoded");[/p]                     }[/p]                          [/p]                     request.send(data_);[/p]                }[/p]                else[/p]                     request.send();[/p]

    this is the code of the plugin suppose if we dont pass hdata then it will run the second condition where it will not insert header variables so that is why your seeing these values. But if you set header then i am sure you will not able to dump this i guess...

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