Solved

Using Ajax from local system

Posted on 2008-10-04
3
1,056 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
  • 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

773 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