Solved

Using Ajax from local system

Posted on 2008-10-04
3
1,052 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
Comment Utility
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
Comment Utility
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
Comment Utility
you are welcome.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now