[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 125
  • Last Modified:

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
0
srbarker8
Asked:
srbarker8
1 Solution
 
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
 
srbarker8Author Commented:
Thx!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now