1

I have a ServiceNow instance running and I'm trying to pull all the users using the JSON Library. I can easily see the JSON list of users if I type in the URL that dumps this data, I'm trying to create a webpage that will take this JSON list and make a list of users out of it. I keep encounter cross domain issues, CORS is not available for me to use therefore I'm trying to dynamically load scripts according to the user's information. This is the code that I'm trying to get working.

        function test() {
        var username = document.getElementById("username").value;
        var password = document.getElementById("password").value;
        var domain = document.getElementById("domain").value;
        var targeturl = "https://" + username + ":" + password + "@"
            + domain + "/sys_user_list.do?JSON&sysparm_action=getRecords";
        $.getScript(targeturl + "&callback=dumpData")
        function dumpData(data) {
            alert(data);
        }
    }

At this point the script seems to load properly, but it causes a syntax error.

Uncaught SyntaxError: Unexpected token :

How am I supposed to make the getScript call so I can get my JSON Data? Is this even a valid method, I stumbled across when trying to figure this issue out and it seems like its my only option. I've tried YQL but that doesn't work.

EDIT:

I have redone the code to make use of the $.ajax, here is the code

            $.ajax({
            url: 'https://' + username + ':' + password + "@" + domain + '/sys_user_list.do?JSON&sysparm_action=getRecords&callback=test',
            type: "GET",  
            success: function (data) {
                alert("hello");
            },
            crossDomain: true,
            dataType: "jsonp",
            jsonpCallback: 'test'
        });
        function test(data) {
            alert("It worked!");
        }

I've scoured all the posts relating to this an still can't figure this one out. Anybody have any advice on how to remedy this situation? It still is given the invalid syntax error. Is it just not possible to get this JSON data?

I also have tried YQL to obtain the data but that encounters security issues as well.

Thanks,

Chris

  • Have you tried defining the callback before the `$.getScript()`? – Corion Jul 02 '13 at 20:54
  • It looks like you're trying to use JSONP, and yes, that should be a valid method. I don't think you've got the syntax quite right - look up [.getJSON](http://api.jquery.com/jQuery.getJSON/) in the jQuery docs. –  Jul 02 '13 at 20:56
  • If I use .getJSON then I get a cross domain error. – Chris Rivadeneira Jul 02 '13 at 21:05
  • Consider invisible characters in your JS or JSON file.. http://stackoverflow.com/questions/12719859/syntaxerror-unexpected-token-illegal – DynamicDan May 22 '14 at 13:12

2 Answers2

1

From my experience using the JSON Web Services plugin on Calgary, ServiceNow does not support JSONP, only JSON.

You may need to build a custom scripted webservice to get the right format.. I don't see JSONP coming in Dublin, either.

Link example: https://<instance>.service-now.com/<table>.do?JSON&sysparm_action=getRecords&displayvalue=true

displayvalue=true swaps sys_id for reference fields to the human-friendly alternative.

To access the data structure, it's as simple as:

function callMe() {

  $.getJSON('url.json', function(myData) {

    for (n = 0; n < myData.records.length; n++) {
      console.log(myData.records[n].<column1> + '\n' +
                myData.records[n].<column2> + '\n' +
                myData.records[n].<column3> + '\n' +
                myData.records[n].<column4> + '\n\n'
               );
    }
  });
};

You might be better off using the CMS/jelly to make this list.

Jake
  • 11
  • 2
0

May be you should try $.ajax({}); instead of $.getSrript(); to do the cross domain e.g $.ajax({url: "your_url.php",dataType; "jsonp",success: function(data){ document.write(data);} });

Stanley Amos
  • 222
  • 2
  • 8
  • So I tried your suggestions and we seem closer, but now I have two slightly different problems. I keep running into a syntax error, for some reason it does not like the : between my user name and password. If I encode the url it then tries to connect to the server by adding http://localhost then the link that i'm trying to hit. But if I enter my url as one string, I get a mime type error. – Chris Rivadeneira Jul 03 '13 at 14:08
  • Mmmh, it's like turning into another unknown error, are you sure you entered the exact `url` ? Or may be the problem is comming from your server `xml file`. You should try and do some research! – Stanley Amos Jul 03 '13 at 14:36
  • I've dug through a lot of the posts relating to this error and they all seem to recommend the same thing and it doesn't work. :( – Chris Rivadeneira Jul 03 '13 at 17:05
  • That means it's coming from your xml file in that actual url you're trying to get respond from. Are you using UTF-8 for the page? – Stanley Amos Jul 04 '13 at 09:07
  • Yes, using UTF-8. The other thing is that it is a JSON object that I'm getting. Not XML. – Chris Rivadeneira Jul 08 '13 at 14:01