Post data to url using httprequest

For developers using the Construct 2 Javascript SDK

Post » Tue Mar 20, 2012 4:10 am

I am trying to use an API to retreive some xml data from a server, i need to post three pieces of data to the url. it tried the code below but it ALWAYS gives the error (i.e unsuccesfull), any ideas where im going wrong. im quite new to javascript

[quote]
?function getinfo()
{

   var httpc = new XMLHttpRequest(); // simplified for clarity
   
   var pdata = 'api_key=b9d57f3fbf2faa8d607ad2f46178ab153cc08b45&game_id=k4scvKUW5&response=XML';//i have used wrong values for security
    
   httpc.onreadystatechange = function ()
   {
    if (httpc.readyState === 4)
     {
     if (httpc.status === 200)
      {
      alert(httpc.responseText);
      }
     else
      {
      alert('There was a problem with the request!!.');
      }
     }
   };
   
       httpc.open("POST", "http://www.apisite.com/api/getInfo", true); // sending as POST

    httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    httpc.setRequestHeader("Content-Length", pdata.length); // POST request MUST have a Content-Length header (as per HTTP/1.1)

    httpc.send(pdata);

};[/quote]rafhelp2012-03-20 04:10:37
B
7
S
1
Posts: 65
Reputation: 821

Post » Tue Mar 20, 2012 7:50 am

I am not skilled enough yet to help you on your script, but why don't you just use the Plugin that is already available?

http://www.scirra.com/forum/plugin-ajaxpost_topic49039.html
B
42
S
9
G
6
Posts: 437
Reputation: 5,626


Return to Javascript SDK

Who is online

Users browsing this forum: Sekigatsu and 0 guests