Home > DeveloperSection > Forums > How can I get jQuery .get to return JSON data?
Andrew Deniel
Andrew Deniel

Total Post:29

Points:203
Posted on    June-20-2013 3:11 AM

 JSON JSON 
Ratings:


 1 Reply(s)
 1120  View(s)
Rate this:
Hi Developers, 

I am trying to fetch information using AJAX from a URL. This URL will return a JSON response but I am having a great deal of trouble getting this to work. I am fairly new to using both AJAX and JSON so I am not quite sure as to what I am doing wrong. I am not receiving any output. Here's what I have so far:

HTML:

<!doctype html>
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content - Type">
    <meta content ="utf-8" http-equiv="encoding">

    <title>My Javascript Practice</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <noscript>JavaScript Must Be Enabled</noscript>
</head>
<body>
    <div id="pub">Parent Div</div>

    <script type="text/javascript" src="getList.js"></script>
</body>
</html>
JavaScript:

var teamId = 883455;
var myUrl = "https://apps-internal.attributor.com/guardianapi/service/csadminservice/csteams/" + teamId + "?view=PUBLISHERS_FOR_TEAM";

$.get(myUrl, function(data){
    $("#pub").html(data);
    alert("load was performed");
});

Any help on above is really appreciated.


Sumit Kesarwani

Total Post:378

Points:2694
Posted on    June-20-2013 6:59 AM

Hi Andrew,

May I suggest to use something like this:

$.ajax({
  type: 'GET',
  url: myURL,
  data: yourDAta,
  dataType: 'jsonp',
  success: function(jsonData) {
    alert(jsonData);
  },
  error: function() {
    alert('Error loading ');
  }
});

Note the usage of jsonp over json


Don't want to miss updates? Please click the below button!

Follow MindStick