Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Ajax form on Client sent to another Server

Posted on 2009-02-12
Medium Priority
Last Modified: 2013-11-19
I need to create an Ajax widget for my resellers where a user can have fill out a form on the client website and post the resulting form to  my server via Ajax.

Example: on www.reseller.com I want a user to click on Add Product.
Add product will dynamically construct an HTML form which is on www.mysite.com.
The user needs to ALWAYS be on www.reseller.com

In other words, www.reseller.com/form_client.html will load the form which is on www.mysite.com/form_server.html.  The user will ALWAYS be on www.reseller.com.

In other words,
www.reseller.com/form_client.html will just read in www.mysite.com/form_server.html
--- Contents of www.mysite.com/form_server.html
// Send the form results
function ajax_post() {}

<form action="javascript:ajax_post" method=GET>
Username: <input type=text name=username><br>
ProductID: <input type=text name=productid><br>
Question by:pmsguy
  • 2
  • 2
LVL 18

Assisted Solution

Morcalavin earned 1500 total points
ID: 23627570
Ajax will not work cross domain.  Ajax request can only be sent/received on the same domain.   If you want to do cross domain ajax, you need to make your request to a script on the same server, and have that script act as a proxy to the other domain.  Thinks like curl in php, httpwebrequests in asp and lwp in perl, etc are needed for this.

Author Comment

ID: 23627634
Do you have any examples on how to accomplish this cross domain process?

I want my clients to install this widget.  I do not want them to have to put any code on their server.
I have no idea if they have PHP, ASP, .. as their server code.

How would I accomplish this?  Do they have to be redirected to my server to fill out the form and I can use ajax to send to my server and when done redirect them back to the original client site?
LVL 18

Accepted Solution

Morcalavin earned 1500 total points
ID: 23632082
If site A wants to get data from site B, then site A cannot get it through ajax(or client side scripting) alone.  This is a violation of the security protocols established for the XMLHttpRequest object.  "Ajax" requests can only be made on the same domain.

The work around is:
www.sitea.com sends a ajax request to www.sitea.com/serversidescript.ext
serversidescript.ext does an http request(similar to an ajax request, but done on the server side, where permissions are usualy capable of doing these requests) on the server to www.siteb.com/datayouwant.
www.sitea.com/serversidescript.ext outputs the results that it gets from www.siteb.com/datayouwant.  So you know have the data from another site, but you are still making ajax request to the same domain.

If reseller.com is the client site, then the only way they would be able to contact your site via ajax is to have a "proxy" script(a script on the server that makes an http request to your site and returns the results) handle the requests.

Author Closing Comment

ID: 31546341
I used an IFRAME scenario to accomplish this goal.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
The viewer will learn how to dynamically set the form action using jQuery.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

564 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