1

I have external json URL.

http://kun6858.iptime.org:8080/apps/list/?app_mb_no=9

And I access this json with jquery $.getJSON(..)

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<script>
$.getJSON(
    "http://kun6858.iptime.org:8080/apps/list/?jsoncallback=?",
    {
        app_mb_no : 9
    },
    function(data) {
        console.log(data);
    }
);
</script>
</body>
</html>

But I can't access the JSON using above source.

I have no idea about how to access external server's json. Does my source have problem? or the JSON?


for your reference, this is screen shot..

enter image description here

Tim Post
  • 33,371
  • 15
  • 110
  • 174
Patrick Jeon
  • 1,684
  • 5
  • 24
  • 31
  • This was secret of Jquery ajax.. http://stackoverflow.com/questions/7936610/json-uncaught-syntaxerror-unexpected-token – Patrick Jeon Jun 05 '12 at 18:13

3 Answers3

0

You need to use JSONP for cross-domain access. Therefore, you have to modify your AJAX call.

An good explaination for this scenario is here:

http://www.jquery4u.com/json/jsonp-examples/

Alexander
  • 23,432
  • 11
  • 63
  • 73
mons droid
  • 1,038
  • 9
  • 10
  • tried with (function($) { var url = 'http://kun6858.iptime.org:8080/apps/list/'; $.ajax({ type: 'GET', url: url, async: false, contentType: "application/json", dataType: 'jsonp', data : { app_mb_no : 9 } }); })(jQuery); this but the same... – Patrick Jeon Jun 05 '12 at 14:01
  • Hi Patrick, does your Server Applikation Supports jsonp? As seen in the Example you need to Wrap your jsonp response in the jsoncallback function. Best regards, Monsi – mons droid Jun 28 '12 at 12:49
0

Three points:

  1. Doesn't app_mb_no need value in your ajax call? e.g. app_mb_no : 9
  2. You can use the .ajax equivalent: $.ajax({ url: url, dataType: 'json', data: data, success: callback });
  3. Have you run your code under a web server? You need to do this.
Tooraj Jam
  • 1,592
  • 14
  • 27
0

I don't know exactly how it works but this solved my problem.

Adding this in ServletResponse

    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setContentType("Content-Type:application/json;charset=UTF-8");

And the Html

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<script>
var url = "http://localhost:8080/apps/list/?app_mb_no=9";


if ($.browser.msie && window.XDomainRequest) {
    // Use Microsoft XDR
    var xdr = new XDomainRequest();
    xdr.open("get", url);
    xdr.onload = function () {
    var JSON = $.parseJSON(xdr.responseText);
    if (JSON == null || typeof (JSON) == 'undefined')
    {
        JSON = $.parseJSON(data.firstChild.textContent);
    }
    processData(JSON);
    };
    xdr.send();
} else {
          $.ajax({
          type: 'GET',
          url: url,
          processData: true,
          data: {},
          dataType: "json",
          success: function (data) { processData(data); }
          });
}

function processData(data) {
    console.log(data);  
}
</script>
</body>
</html>

If anyone knows better way please teach me!! thanks

Patrick Jeon
  • 1,684
  • 5
  • 24
  • 31