1

I've got form information captured and have built a URL to another website. How do I actually send that from javascript.

[theotherwebsiteUrl]?parama=A&paramb=B&paramc=C

This is the format I have built as a string into a variable called body.

I want 'some JS function' to send the contents of body to [theotherwebsite].

Their server is geared to accept this string and parse if from my website. I just don't know what function to use to ship off an entire url string variables and all now that I concatenated the string to their format.

Thank you!

Alex Hadley
  • 2,125
  • 2
  • 28
  • 50
  • AJAX / XmlHttpRequest - but beware: you'll have problems trying to get data back from a different domain. – bfavaretto Sep 04 '12 at 21:59
  • Why use javascript when you can use the HTML `form`'s `get` or `post` methods? Similarly, why not use an AJAX call and send the variables that way? Can you share some code so we can better assess the question? – Shabab Sep 04 '12 at 22:01
  • I would consider using a proxy as my *preferred* choice, where applicable. However, one "trick" *if just needing to invoke a GET and the HTTP response itself is not important*, is to use a non-XHR method (e.g. image URL or script SRC), which will work cross-domain everywhere. It's almost like [JSONP](http://en.wikipedia.org/wiki/JSONP) (keyword) works this way .. (JSONP provides results by side-effect of the ` –  Sep 04 '12 at 22:07
  • The string is too long for here, but here is most of it as they say me they need to receive it: var body=url + "firstName="+ firstName + "&lastName=" + "&lastName=" + lastName +"&sportID=" + sportID + "&entryFrom=" + entryFrom + "&linkSource=" + linkSource + "&primaryEmail=" + primaryEmail +"&graduationYear=" + graduationYear + "&postalCode=" + postalCode + "&parent1FirstName=" + parent1FirstName + "&parent1LastName=" + parent1LastName + "&parent1Relationship=" + parent1Relationship+ "&parent1Phone1=" + parent1Phone1 + "&parent1Phone2=" + parent1Phone2 + "&parent1Phone3="+ parent1Phone3"; – FreedomRoadPublishing Sep 04 '12 at 22:07

1 Answers1

2

You "send information you another website" by loading a URL (sending an HTTP request), either directly in the browser or through Ajax. You can attach GET parameters to that URL or send POST data along with it.

For example:

window.location.href = '[otherWebsiteUrl]?parama=A&paramb=B&paramc=C';

or:

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","[otherWebsiteUrl]?parama=A&paramb=B&paramc=C",true);
xmlhttp.send();

Note: sending a request through Ajax to a different server requires the remote server to be set up to receive it - see How do I send a cross-domain POST request via JavaScript?

Or cheat a bit, using JSONP: http://en.wikipedia.org/wiki/JSONP

Community
  • 1
  • 1
Robin Winslow
  • 10,908
  • 8
  • 62
  • 91
  • window.location.href worked like a champ. I just received a response email from their server indicating a positive submission was received. THANK YOU!! I might just come here to worship. I haven't received this much good help in one place in my life. You guys/gals are severely top shelf. THANK YOU, again!!! – FreedomRoadPublishing Sep 04 '12 at 22:38