Solved

Using Ajax from local system

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pass foreach data in php into jquery ajax 10 45
ASP get fieds value 2 20
Link Anchor Not working 9 46
Add - in a loop to first and last element. 6 36
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

740 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