Solved

GET JSONP

Posted on 2014-11-10
5
119 Views
Last Modified: 2014-11-26
Need help loading data when the URL contains the callback

myURL = 'http://comcat.cr.usgs.gov/fdsnws/event/1/query?starttime=2014-10-01%2000:00:00&minmagnitude=6&format=geojson&callback=eqfeed_callback&endtime=2014-10-31%2023:59:59&orderby=time';

$.ajax({
   type: 'GET',
    url: myURL,
    async: false,
    jsonpCallback: 'eqfeed_callback',
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(data) {
       console.log(data.features.properties.mag);
    },
    error: function(e) {
       console.log(e.message);
    }
});

Open in new window

0
Comment
Question by:lvmllc
  • 3
  • 2
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40434261
test page : http://jsfiddle.net/8ujqLy4v/

function eqfeed_callback(data) {
    for(var i=0;i<data.features.length;i++) {
       alert(data.features[i].properties.mag);
    }
};
var myURL = 'http://comcat.cr.usgs.gov/fdsnws/event/1/query?starttime=2014-10-01%2000:00:00&minmagnitude=6&format=geojson&callback=eqfeed_callback&endtime=2014-10-31%2023:59:59&orderby=time';

var s = document.createElement("script");
s.type = "text/javascript";
s.src = myURL;
document.head.appendChild(s);

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 40434265
0
 

Author Comment

by:lvmllc
ID: 40435451
So if it works to build this with jQuery, why is it that when copying the URL to a script tag in the head from the start it fails?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 40435565
not sure you got it.
without jQuery it work (my test page)
using jQuery.ajax, jQuery.getJSON, jQuery.getScrip or any other jQuery fail
0
 

Author Comment

by:lvmllc
ID: 40457440
Sorry for not getting back to this sooner.
Just confirming if the result had been a simple file of data starting out with  eqfeed_callback({"type":"FeatureCollection","metadata" ...  then I could have just used a <script src="myscripts.js"></script>  in the head. But since this is a call to a file that is being generated I need the let the script know to read the result as JavaScript - thus    s.type = "text/javascript";
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now