Goole Translation API: Method Post

94 views
Skip to first unread message

hknight

unread,
Jan 30, 2010, 6:56:05 PM1/30/10
to Google AJAX APIs
How can I change the code for Google Translation from using GET method
to POST method for requests?


You can download my code here:
http://sites.google.com/site/d9qjx79zqpo3/zzz1a.html?attredirects=0&d=1


---


<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></
script>
<script type="text/javascript">
document.write('<style type="text/css">.textContent {visibility:
hidden; display:block;}</style>');
google.load("language", "1");
</script>
<style type="text/css">
span{display: none;}
</style>
</head>
<body>

<div class="textContent">1. One</div>
<div class="textContent">2. Two</div>
<div class="textContent">3. Three</div>
<div class="textContent">4. Four</div>
<div class="textContent">5. Five</div>
<div class="textContent">6. Six</div>
<div class="textContent">7. Seven</div>
<div class="textContent">8. Eight</div>
<div class="textContent">9. Nine</div>
<div class="textContent">10. Ten</div>
<div class="textContent">11. Eleven</div>
<div class="textContent">12. Twelve</div>
<div class="textContent">13. Thirteen</div>
<div class="textContent">14. Fourteen</div>
<div class="textContent">15. Fifteen</div>
<div class="textContent">16. Sixteen</div>
<div class="textContent">17. Seventeen</div>
<div class="textContent">18. Eighteen</div>
<div class="textContent">19. Nineteen</div>
<div class="textContent">20. Twenty</div>
<div class="textContent">21. Twenty One</div>
<div class="textContent">22. Twenty Two</div>
<div class="textContent">23. Twenty Three</div>
<div class="textContent">24. Twenty Four</div>
<div class="textContent">25. Twenty Five</div>
<div class="textContent">26. Twenty Six</div>
<div class="textContent">27. Twenty Seven</div>
<div class="textContent">28. Twenty Eight</div>
<div class="textContent">29. Twenty Nine</div>
<div class="textContent">30. Thirty</div>


<script type="text/javascript">
var stuffToChange = (document.getElementsByClassName)?
document.getElementsByClassName('textContent'):_getElementsByClassName
('textContent');
function translateThis(lang,iii) {
if(lang) {
var iiiI=0;
if(!stuffToChange[iii].innerHTMLO) { stuffToChange[iii].innerHTMLO
= stuffToChange[iii].innerHTML;}
google.language.translate(stuffToChange[iii].innerHTMLO+'
<span>'+Math.random()+'</span>', "en", lang, function(result) { if
(result.translation) {myfunction(result.translation,iii)};
});
}
}
function myfunction(rs,cnt)
{
var translated = document.getElementById("translation");
stuffToChange[cnt].innerHTML=rs;
stuffToChange[cnt].style.visibility='visible';
if(cnt<=stuffToChange.length) {translateThis('en',+
+cnt); }

}
if (! document.getElementsByClassName) {
function walkTheDOM (node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function _getElementsByClassName (className) {
var results = [];
walkTheDOM(document.body, function (node) {
var a, c = node.className, i;
if (c) {
a = c.split(' ');
for (i=0; i<a.length; i++) {
if (a[i] === className) {
results.push(node);
break;
}
}
}
});
return results;
}
}
translateThis('en',0);

</script>

</body>
</html>

Jeremy Geerdes

unread,
Jan 30, 2010, 7:02:12 PM1/30/10
to google-ajax...@googlegroups.com
You can't tell the pre-written JS API to use the POST method because of the nature of the API. Namely, the only way to do so would be via XMLHttpRequest, but because the information is coming from Google, the browser's same-origin policy (SOP) will stop you. So you have to write your own server-side proxy and JS wrapper. Using the JS wrapper, you will create XMLHttpRequest objects and submit requests to the server-side proxy, which will run the query against the Google API and return the information you need. Finally, you would parse that information and utilize it in your own application.

Jeremy R. Geerdes
Effective website design & development
Des Moines, IA

For more information or a project quote:
http://jgeerdes.home.mchsi.com
jrge...@gmail.com

If you're in the Des Moines, IA, area, check out Debra Heights Wesleyan Church!

> --
> You received this message because you are subscribed to the Google Groups "Google AJAX APIs" group.
> To post to this group, send email to google-ajax...@googlegroups.com.
> To unsubscribe from this group, send email to google-ajax-searc...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-ajax-search-api?hl=en.
>

Vision Jinx

unread,
Jan 30, 2010, 7:36:02 PM1/30/10
to Google AJAX APIs
You can check my page here also

http://www.visionjinx.net/miniapps/glang.htm

if you need a visual sample (view source). I just used some custom
javascript wrappers for the basic ajax things and such but the code
should be pretty straight forward ajax and js stuff. (like xhr =
XMLHttpRequest objects)

Just as Jeremy stated you will see how I use AJAX POST requests to
query a server side script (PHP in this case) to make the remote
request to Googles RESTful apis.

http://code.google.com/apis/ajaxsearch/documentation/#fonje_snippets

Cheers!
Vision Jinx

Reply all
Reply to author
Forward
0 new messages