cancel
Showing results for 
Search instead for 
Did you mean: 
belgort
Surveyor

Parse error on API call

PLEASE REMOVE THIS QUESTION

I'm using JavaScript to try and retrieve the list of courses and am receiving a jQuery parse error when using $.getJSON.  I have attached my code for your review.

I modified the access_token to protect the innocent 🙂 I also verified that the JSON being returned is in fact valid.

Thank you in advance for your assistance.

Labels (1)
Tags (3)
11 Replies
wmonroe
Community Member

I'm trying to use the API from JavaScript as well.  I am trying to use an jquery ajax call:

function getCourses(){

  $.ajax

  ({

    type: "GET",

    url: "https://canvas.instructure.com/api/v1/courses",

  // beforeSend: function (xhr) {

  //   xhr.setRequestHeader ("Authorization", "XXXX");

  // },

    headers: {

      "Authorization": "XXXXXX",

      "Access-Control-Allow-Origin": "*"},

    dataType: 'json',

    async: false,

    success: function (){

    console.log(data);

    }

});

}

I get a cross domain errors.

It would be really helpful to have an example of how to get API calls working from client side JavaScript.

kent
Community Member

I would guess you should  play around with jsonp instead of json for datatype in your javascript if the cross domain errors are persistent. There's lots of documentation online about it. But I could be wrong.

tjones1
Community Member

Here is how I'm using jsonp:

I'm trying to GET an Assignment

$.ajax({

            type: "GET",            // Type of request to be send

            url: canvasapi,          // url+token to which the request is sent

            contentType: "application/json; charset=utf-8",

            dataType: "jsonp",     // type of data

            success: function(data, status, jqXHR)

            {

                console.log( "success status: "+status );

                console.log( "success data: "+data );

                console.log( "success xhr text: "+jqXHR.responseText );

            },

            jsonp: false,            // should use Callback below

            jsonpCallback: function(data)

            {

                //var asgndata = JSON.parse(data);

                //console.log( "jsonp asgndata: "+asgndata );

                console.log( "jsonp data: "+data );// undefined - parsererror in returned data

            },

            error: function (jqXHR, status)

            {

                console.log( "ERR status: "+status );

                console.log( "ERR xhr text: "+jqXHR.responseText );

            }

});

It makes it past the CORS cross domain

But I'm getting a parser error so it data is undefined

Screen Shot 2015-07-09 at 11.01.38 AM.png

This is the SAME exact error I am getting.

tjones1
Community Member

I tried a different API call to see if it caused an error.

Screen Shot 2015-07-10 at 8.21.52 AM.png

It did.

I've added a new question that may help us get some answers.

How do you call the API from an external server?

I do have the API's working in PHP. Just not in JS.

tjones1
Community Member

I learned to cURL today. Much better.

kent
Community Member

Curl is cool indeed!

stuart_ryan
Community Coach
Community Coach

Hi  @belgort ,

I am going through having a look at some of the early days in the Canvas Developers group, and checking in to see if older enquiries have been answered. I also noticed there hasn’t been any discussion on this question in quite some time.

I am wondering, were you ever able to resolve this with the assistance provided? I am hoping I can assume that it is well and truly resolved by now, but if not, please let us know and we can certainly have another look. Alternatively, if have some insights you may be able to share for others that would be awesome too!

I will mark this as assumed answered for the time being, however, by all means please let us know if you still have an outstanding question and we will take a peek!

Cheers,
Stuart