By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,963 Members | 1,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,963 IT Pros & Developers. It's quick & easy.

How to send javascript-objects and arrays to the server

P: 2
First I want to excuse me for my bad english. I have programmed a javascript application, like a file browser. You can create move, delete and create files and folders. The data is saved in a node-structure. Each node contains a title and if it is a folder it contains a array of subnodes. My javascript filebrowser application works very well, but how can i send this structure to the server (I use ASP.NET).
I tried already to send the data via a hidden variable, but this is very complex. And the Cookie-Methode i think are also not the most professional solution.

Are there alternatives how a client-side javascript-application can communicate with the server?

thanks for any answers

Humer Thomas
Oct 17 '06 #1
Share this Question
Share on Google+
2 Replies


YenRaven
P: 29
javascript can communicate with a server with a technology called Ajax. basically what this does is allow you to open a server page without the browser actually displaying the data. you can pass data to the server page in the url variables(Exp. someplace.aspx?var1=this&var2=somethingelse). although i have little expiriance useing ajax i do have this script i modified that makes it fairly simple.

Expand|Select|Wrap|Line Numbers
  1. var xmlHttp
  2. var responseArea
  3. var ResponseText = " "
  4. var outputAs
  5. var complete = true
  6.  
  7. function trimToResponse(){
  8. var trimStart = ResponseText.indexOf("<body>\r\n")
  9. var trimStop = ResponseText.indexOf("\r\n</body>")
  10. ResponseText = ResponseText.substr(trimStart + 8, trimStop)
  11. return ResponseText;
  12. }
  13. function testConn()
  14. {
  15. alert("Connection Suscessful!")
  16. }
  17. function ajax(str, page, responseID, output)
  18. complete = false
  19. if(output != ""){
  20. outputAs = output
  21. }else{
  22. outputAs = "var"
  23. }
  24. if(responseID != ""){
  25. responseArea = responseID
  26. }else{
  27. responseArea = "null"
  28. }
  29. //document.write(responseID)
  30. if (str.length > 0)
  31. if(page.indexOf("?") == -1)
  32. {
  33. var url=page + "?sid=" + Math.random() + "&data=" + str
  34. }
  35. else
  36. {
  37. var url=page + "&sid=" + Math.random() + "&data=" + str
  38. }
  39. xmlHttp=GetXmlHttpObject(stateChanged)
  40. xmlHttp.open("GET", url , true)
  41. xmlHttp.send(null)
  42. else
  43. if (outputAs == "ID"){
  44. document.getElementById(responseArea).innerHTML=""
  45. }else{
  46. ResponseText=""
  47. }
  48.  
  49. function stateChanged() 
  50. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  51. if (outputAs == "ID"){
  52. document.getElementById(responseArea).innerHTML=xmlHttp.responseText
  53. }else{
  54. ResponseText=xmlHttp.responseText
  55.  
  56. }
  57. complete = true;
  58.  
  59. function GetXmlHttpObject(handler)
  60. var objXmlHttp=null
  61.  
  62. if (navigator.userAgent.indexOf("Opera")>=0)
  63. {
  64. alert("This example doesn't work in Opera") 
  65. return 
  66. }
  67. if (navigator.userAgent.indexOf("MSIE")>=0)
  68. var strName="Msxml2.XMLHTTP"
  69. if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
  70. {
  71. strName="Microsoft.XMLHTTP"
  72. try
  73. objXmlHttp=new ActiveXObject(strName)
  74. objXmlHttp.onreadystatechange=handler 
  75. return objXmlHttp
  76. catch(e)
  77. alert("Error. Scripting for ActiveX might be disabled") 
  78. return 
  79. if (navigator.userAgent.indexOf("Mozilla")>=0)
  80. {
  81. objXmlHttp=new XMLHttpRequest()
  82. objXmlHttp.onload=handler
  83. objXmlHttp.onerror=handler 
  84. return objXmlHttp
  85. }
the function you would want to use is this

ajax(data, page, output, responseID)
str: a piece of data to be sent to the server page as the variable "data"
page: a string for the url of the server page
output: optional; a string either "ID" to output data from the server page to a span tag or "var" to output to the ResponseText variable. default is "var".
responseID: optional; a string that represents the span tag's id to output data to.
Oct 17 '06 #2

P: 2
thank you very much, i think that will be a good solution for my problem.
Oct 17 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.