2

i using JQuery Ajax to call REST api in jsp but it return null no matter how i call but it can work in html. is there any way to solve this problem. Cant seem to find a solution in the net.


$(document).ready(function () {
    alert('ready');
    var accessKey = 'xkg8VRu6Ol+gMH+SUamkRIEB7fKzhwMvfMo/2U8UJcFhdvR4yN1GutmUIA3A6r3LDhot215OVVkZvNRzjl28TNUZgYFSswOi';
    var thisUrl = 'http://www.onemap.sg/API/services.svc/getToken?accessKEY=' + accessKey;
    $.ajax({
        type: "GET",
        url: thisUrl,
        dataType: 'application/json',
        success: function (data) {

            alert('data is:' + data.GetToken[0].NewToken);
        }
    });
    alert(thisUrl);
});
Hoon
  • 115
  • 1
  • 11

2 Answers2

0

dataType should be jsonp

$(document).ready(function () {
    var thisUrl = 'http://www.onemap.sg/API/services.svc/getToken?accessKEY=' + accessKey;
    $.ajax({
        type: "GET",
        url: thisUrl,
        dataType: 'jsonp',
        success: function (data) {
            console.log(data)
            alert('data is:' + data.GetToken[0].NewToken);
        }
    });
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • hi, it still return undefined. – Hoon Nov 01 '13 at 02:25
  • @user2627054 add a `console.log(data)` and see what is there in the `data` – Arun P Johny Nov 01 '13 at 02:26
  • You can't just use the code that @ArunPJohny has given you. He is hard-coding an accessKey of 3. His code works but returns an invalid access code error – jasonscript Nov 01 '13 at 02:38
  • @jasonscript any suggestion on what i can do? – Hoon Nov 01 '13 at 02:42
  • You can use his code, but you have to make sure that you're passing in the correct accessKey value. You are setting it in your code too, but you must have a global variable somewhere. I just wanted you to know you can't just copy and paste his code directly and expect it to work – jasonscript Nov 01 '13 at 02:48
  • @user2627054 you need to register for the service and a proper accessKey for the service and use it – Arun P Johny Nov 01 '13 at 02:49
  • I just update the code with my accesskey. This is a proper accesskey that will return json. for some reason it cannot work in jsp file but it can work in html. – Hoon Nov 01 '13 at 02:54
  • @user2627054 you have to see what are the contents of `data` for that you need to use the `console.log()` I suggested.... add it to the code and check the browser console to see what is logged – Arun P Johny Nov 01 '13 at 02:55
  • Your datatype needs to be `jsonp` because this is a cross-domain javascript request. See here http://stackoverflow.com/questions/2067472/what-is-jsonp-all-about – jasonscript Nov 01 '13 at 02:56
  • Hi, Thanks for the help. But do u have any ideas why is dont work in jsp but work in html. – Hoon Nov 01 '13 at 03:34
0

Refer to this article: http://www.isgoodstuff.com/2012/07/22/cross-domain-xml-using-jquery/

You only need "jquery.xdomainajax.js" that is there in the sample source-code to make it work.

$.ajax({
          url: 'https://asdf/asdf',
          dataType: "xml",
          type: 'GET',
          success: function(res) {
          var myXML = res.responseText;
          alert(myXML);
        }
      });
Bharthan
  • 1,458
  • 2
  • 17
  • 29