0

I have a requirement in which i need to get the content generated in a particular url (which is in JSON). I need to assign this output to my json object after my jQuery ajax call.I've tried this by using the following code, but not yeilding any result (always getting null within the success function parameter).

My Jquery ajax call code is as follows,

     $.ajax({
       type: "GET",
       url: "http://some url",
       data : {},
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       success: function (obj) {               
           alert(obj);
           var test = eval("(" + obj + ")");             
           alert(test);        

       },
       error: function () {
           alert("error");
       }
   })

Is there any error in my request? If this is not the right method please do suggest a method for my situation.

Harun
  • 5,109
  • 4
  • 38
  • 60

1 Answers1

0

You've got a couple problems here. First, you won't be able to alert it. Use

console.log(obj);

And then check it out in Firebug.

As for your request, you need to iterate through them (and you can exclude data altogether if it is empty)...

     $.ajax({
       type: "GET",
       url: "http://some url",
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       success: function (data) {   
               $.each(data, function(idx, obj){    
                    console.log(obj);
               });              
       },
       error: function () {
           alert("error");
       }
   })

That should work if the URL is actually giving you valid json.

Calvin Froedge
  • 16,135
  • 16
  • 55
  • 61
  • The firebug console too shows the object null as i got it earlier with alert(). My url is yielding a valid json. – Harun Jun 20 '11 at 07:14
  • Are you using console.log within the loop? Can you post the code that creates the json output? – Calvin Froedge Jun 20 '11 at 13:22
  • The problem was, i tried to sent the url request to a different server and this can't be done using jquery ajax. – Harun Jun 28 '11 at 13:30