Solved

Using Ajax from local system

Posted on 2008-10-04
3
1,060 Views
Last Modified: 2010-04-21
Greetings,
I'm developing some HTML/JavaScript based training and I would like to gather user comments using Ajax to send data to my clients server. However, one of the requirement here is that the HTML content must be able to run on a local system using the file:// protocol. I have it working in IE7 such that a form is being posted to a Java servlet, and the response is being returned without a problem. However, in Firefox this does not work, it returns an access denied error. I'm currently using the Dojo JavaScript framework to do this but I expect that this is an issue with Ajax and Firefox rather then with Dojo.

Is what I'm trying to do here with Ajax even valid? Should I be able to use Ajax to make a call to a separate server from a local system? This does work if I'm using Ajax to read a file from the local system.

If what I'm doing is not valid then what are my options? I know that I could use an Iframe to load the server response in but I would like a cleaner solution.

Any help here would be greatly appreciated.
Thanks,
dv
0
Comment
Question by:vanfleet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22643392
strictly speaking the browser should NOT allow you to post data to the server via ajax since you are essentially doing cross-site scripting. However, IE always does thing its own way. Long story short, FF is doing what it is supposed to, so you will not be able to send/post the data via ajax with FF. However, if instead of ajax you were using a form, then you would be able to post/send the data to another domain without any restrictions. To avoid refreshing the page, make sure the form targets a hidden frame.Thus, my suggestion is as follows:
Assuming you are currently sending this via ajax:
user=3&sport=football

then create a form with hidden fields:
<form action="http://www.remoteserver.com" method="post" id="theForm" target="hiddenFrame">
<input type="hidden" id="user" name="user" value=""/>
<input type="hidden" id="sport" name="sport" value=""/>
</form> 
and the corresponding hidden frame:
<iframe id="hiddenFrame" name="hiddenFrame" src="" style="position:absolute;left:-100em;"/> 
then as you get the values that you need to send, update the form fields -ex:
document.getElementById("user")="joe";
document.getElementById("sport")="football"; 
and finally when you are ready to send the data, then submit the form:
document.getElementById("theForm").submit();

Open in new window

0
 

Author Closing Comment

by:vanfleet
ID: 31503132
hielo, Thank you very much for your help. I didn't want to have to do this with an IFrame but it looks like that is my best option, considering my constraints. Thanks for the clarification.
dv
0
 
LVL 82

Expert Comment

by:hielo
ID: 22660340
you are welcome.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question