Hello,
I would like my game (written in JS) to be able to send a high score, username, and password to a Rails server. Since I would like to use a REST-based interface to the server, I think my best option is to use XMLHttpRequest to POST the score (rather than using the WWW class which I believe only supports GET). I do not want to encode any information in the URL. Does anyone have any code snippets for using XMLHttpRequest in JS? Is it supported? If not, will it be in the upcoming 2.0 version of Unity?
Thanks,
N
(I haven’t actually tried the following approach, but it seems plausible!)
I am pretty sure the Unity player doesn’t support XMLHttpRequest but it does allow you to call JavaScript code in the surrounding web page.
As you probably know, Rails handles its AJAX stuff more or less transparently using partials. You could specify a partial containing a form with high score, name and password fields and then use some CSS to render it invisible onscreen. I think you should be able to fill in the form’s details and post it using some JS that you could call from the web player. If the web server needs to return any data, then it could put that in the (invisible) update to the partial. You could have another JS function in the page that uses the DOM to extract data from the partial. If you call this function from Unity then you have limited two-way communication.
I would approach this by creating a visible version of the form in an ordinary web page using the usual Rails AJAX techniques (partials, etc). Then code and test the JS functions to fill in and submit the form. Then, put a Unity player in the page which calls the appropriate JS and add some CSS to hide the partial.
The PragProg “Agile Web Development” book has got good tutorials and reference information about using AJAX with Ruby. You might also be able to use some RJS to render your special JavaScript into the web page, but it might be easier just to poke around in the generated page source to find the right names and code the JS yourself.
var xmlHttpRequest;
function sendScores(playerName, score)
{
xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open('http://domain/path/file.ext');
xmlHttpRequest.onreadystatechanged = sendScores_handler;
xmlHttpRequest.send('playerName=' + playerName + '&score=' + score);
}
function sendScores_handler()
{
if(xmlHttpRequest.state = 4 xmlHttpRequest.status = 200)
{
// the rest is generic, and you can do a JS Callback to the Web Player
// to let the user know visually that the score was added, and maybe
// refresh the scoreboard data from the returned values ?
}
}
That snippet would generate a standard HTTP POST request with the “form” values “playerName” and “score” – how Ruby/Rails handles this… I’m not sure, in PHP it would be _POST["playerName"] and _POST[“score”] … in .NET it would be Request.Form[“playerName”] and Request.Form[“score”] (I believe)
I’m going to put this JS code into a partial and give it a go. Thanks to both!
i attached my Stock .json file
below i m attaching my script file named as Jsontrry.js
i want to read and write file content using json parsing …please help
2128707–140049–Stock .txt (467 Bytes)
2128455–140016–Jsontryy.js (4.29 KB)