how to make jsonp calls dynamically

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>

Open in new window


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
srbarker8Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Are you able to use a library such as jQuery to do your JSONP requests?
0
srbarker8Author Commented:
Hi ... yes, I can ...

If there is an easier approach using jquery, I would appreciate some help with that approach.

Thx Scott
0
leakim971PluritechnicianCommented:
you can use this : http://easyxdm.net/wp/
check the examples and what it do
0
RobOwner (Aidellio)Commented:
jQuery will be the easiest:

include it in your <head> like this: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

then it's as simple as this
$(function() {
	$('#demo3').tagit({
		tagSource:function( request, response ) {
			$.ajax({
				url: "http://gd.geobytes.com/AutoCompleteCity",
				dataType: "jsonp",
				data: {
					q: request.term
				},
				success: function( data ) {
					response( data );
				}
			});
		},
		triggerKeys:['enter', 'comma', 'tab'],
		allowNewTags: false
	});
});

Open in new window


http://jsbin.com/wavilu/4/edit
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srbarker8Author Commented:
Thx!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.