Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

how to make jsonp calls dynamically

Avatar of srbarker8
srbarker8Flag for Canada asked on
JavaScriptAJAX
5 Comments1 Solution146 ViewsLast Modified:
Hi there,

I need to be able to have one of my web server pages get data (on user request) from another server of mine....so the same origin policy is the problem I am working around here.  

I have used the following javascript approach to make a single cross domain call to the other server.  I use a callback parameter as shown in numerous examples out there.  And I use the approach of creating a script element and insert the script into the page head....as shown here:

A sample page on www.mydomain.com...

<script>
	function processJSON (inData) {

	  // Do something with the JSON response
	  $("#results_div").html(inData);
	}	
	
	// Create a new script element
	var script_element = document.createElement('script');
	// Set its source to my target server
	script_element.src = 'http://frm.mydomain.com/calls/cpc2.go?jsoncallback=processJSON';
	// Put the script element in the page head
	document.getElementsByTagName('head')[0].appendChild(script_element);	
</script>

For me, this is a simple way to get back some html sub-components from another server of mine.

So, my question is this:  How do I do this multiple times?   This technique will continue to append script elements into the page head in order to get them to fire and that seems rather unhealthy, yes?  To continue to plug more and more of them into the head?

I have a few pages in my system where staff may make 10 or twenty such calls and I am looking for some assistance as to the best way to do this with this jsonp approach.  Essentially have this associated with a button or link that when clicked will accomplish a cross-domain task like this over and over again upon request.

Many thanks, in advance, for any assistance in this regard.

Scott