Results 1 to 2 of 2

Thread: Need javascript help to post and retrieve xml data from url

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Need javascript help to post and retrieve xml data from url

    Ok the site is score oid and i need to get some information about a game. the server returns data in xml format.

    I have tried many different code but cant get it to return anything successfully.

    OK here is the code im using (for security reasons i have changed the correct key values, you could test the script with your existing php etc..)

    Code:
    function getgame()
    {
    
    var url ="http://www.scoreoid.com/api/getGame";
    var request;
    
    var params ='api_key: "b9d57f3fbf2faa8d607ad2fc6178ab153gt08b48", game_id: "k4scvJUW2", response:"XML"';
    
    request = new XMLHttpRequest();
    
    request.open("get",url,1);
    
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
    request.setRequestHeader("Content-Length", params.length); // POST request MUST have a Content-Length header (as per HTTP/1.1)	
    		
    request.onreadystatechange = function()
                {
                    if (request.readyState === 4) 
    				 {
                     if (request.status === 200 ) 
    				  {
                      alert(request.responseText);//getAllResponseHeaders());
                      }
                     else 
    				  {
                      alert("ERROR");
                      };
                     };
                };
    		request.send(params);
    
    }
    it should return a xml data structure with the info i need.

    The API getGame on the server needs three variables posted api_key, game_id and response


    any help appreciated. and please i have given it a lot of tries, im new to javascript



    for testing purposes i made a html file with the function to see if the javascript works:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title></title>
    <meta name="" content="">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
    
    
    <script type="text/javascript">  
    function getgame()
    {
    
    var url ="http://www.scoreoid.com/api/getGame";
    var request;
    
    var params ='api_key: "b9d57f3fbf2faa8d607ad2fc6178ab153cc08b48", game_id: "k4scvKUW3", response:"XML"';
    
    request = new XMLHttpRequest();
    
    request.open("get",url,1);
    
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-Length", params.length); // POST request MUST have a Content-Length header (as per HTTP/1.1)	
    		
    request.onreadystatechange = function()
                {
                    if (request.readyState === 4) 
    				 {
                     if (request.status === 200 ) 
    				  {
                      alert(request.responseText);//getAllResponseHeaders());
                        //this.data = request.responseText;
                      }
                     else 
    				  {
                      alert("ERROR");
                      };
                     };
                };
    		request.send(params);
    
    }
        </script> 
    	
    	 
        </head>  
    <body>
    <button onclick='getgame();'>PRESS</button>
    </body>
    </html>

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Re: Need javascript help to post and retrieve xml data from url

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title></title>
    <meta name="" content="">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
    
    
    <script type="text/javascript">  
    function getgame()
    {
    
    
    var request;
                           
    //var param;// ={api_key: "b9d57f3fbf2faa77607ad2fc6178ab156cc08b48", game_id: "k4s777UW3", response:"XML"};
    //var api_key="b9d57f3fbf2faa8d607ad2fc6178ab153cc08b48";
    //var game_id="k4scvKUW3";
    //var response="XML";
    
    //param= new Array(new Array("api_key", "b9d57f3fbf2faa8d607ad56178ab153cc08b48"), new Array("game_id", "k4s77KUW3") , new Array("response", "XML"));
    
    request = new XMLHttpRequest();
    
     // POST request MUST have a Content-Length header (as per HTTP/1.1)	
    		
    request.onreadystatechange = function()
                {
                    if (request.readyState === 4) 
    				 {
                     if (request.status === 200 ) 
    				  {
                      alert(request.responseText);//getAllResponseHeaders());
                        //this.data = request.responseText;
                      }
                     else 
    				  {
                      alert("The ERROR : "+request.responseText);
                      };
                     };
                };
    			
    request.open("post","http://www.scoreoid.com/api/getGame",1);
    
    /*var formdata = new FormData();
    formdata.append("api_key", "b9d57f3fbf2faa8675ad2fc6178ab153cc08b48"); 
    formdata.append("game_id", "k4scv77W3");
    formdata.append("response", "XML");*/
    
    var boundary;
    
    boundary = "Boundary_" + new Date().getMilliseconds() + ";";
    
    var body = setBody("api_key", "b9d57f3fbf2faa8d607ad2fc6178ab576cc08b48",boundary);
    body += setBody("game_id", "k4scv77W3",boundary)
    body += setBody("response", "XML",boundary)
    //...
    body += "--" + boundary; //end of body
    
    
    //request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    request.setRequestHeader("Content-Type","multipart/form-data; boundary="+boundary+"; charset=UTF-8");
    request.setRequestHeader("Content-Length", body.length);
    //request.setRequestHeader("Connection", "close"); 
    	
    request.send(body);
    
    }
    
    function setBody(name, value,bb)
    {
    var body = '--' + bb + '\r\n';
    body += 'Content-Disposition: form-data; name="' + name + '"' + '\r\n\r\n';
    body += value + '\r\n';
    return body;
    }
        </script> 
    	
    	 
        </head>  
    <body>
    <button onclick='getgame();'>PRESSit</button>
    </body>
    </html>
    this does not work either, going round in circles im gonna forget it, this langauge is not straight forward enough its like theres is too many parameters and reliance on other things which you may not be aware of.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width