Ajax plugin to send header variables

For developers using the Construct 2 Javascript SDK

Post » Wed Feb 12, 2014 7:09 am

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

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

[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", "hdj2723");
                         request["setRequestHeader"]("X-Token", "80e68a4679e864ec99058b609d29f34523e");
                         
                         
                    //     alert("HDPOST "+request["setRequestHeader"]);
                         
                    }
     
                    request.send(data_);
               }


[/code]



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


[code]
<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", "[email protected]");
client["setRequestHeader"]("X-Number", "hdj2723");
client["setRequestHeader"]("X-Token", "80e68a467969058b609d29f34523e");
     
client.send("[email protected]");
}

fetchStatus("http://localhost/myrestapi");

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

Post » Wed Feb 12, 2014 7:46 am

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 :)
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Wed Feb 12, 2014 7:52 am

@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 :(
B
9
S
2
Posts: 108
Reputation: 1,010

Post » Wed Feb 12, 2014 8:01 am

edit: nvm, doing new test, unpacked wronglennaert2014-02-12 08:04:51
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Wed Feb 12, 2014 8:21 am

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.lennaert2014-02-12 08:33:49
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Wed Feb 12, 2014 1:50 pm

@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 http://localhost:50000/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???
B
9
S
2
Posts: 108
Reputation: 1,010

Post » Wed Feb 12, 2014 2:18 pm

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.
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Wed Feb 12, 2014 2:27 pm

Can you post an example of what to input in the HData entry field ?
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Wed Feb 12, 2014 2:39 pm

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.
B
9
S
2
Posts: 108
Reputation: 1,010

Post » Wed Feb 12, 2014 2:41 pm

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.
B
9
S
2
Posts: 108
Reputation: 1,010

Next

Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 2 guests