Getting unexpected token error while consuming web service in sharepoint

Shashank2235
Shashank2235 used Ask the Experts™
on
I am trying to consume a cross domain  restfull  web service from SAP of datatype jsonp in sharepoint online using ajax .I am trying to print the output in console but i am getting unexpected token .When i investigate more i can see the url followed by

"?callback=jQuery3110512…_1489476298510&_=1489476298511:1 " when i click on it  i get the output .I am missing something here Pls can some one help
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
JSONP means the return is wrapped in a function that has to be called

In this case you are saying the callback is ?callback=jQuery3110512…_1489476298510

Which means in the code that is returned from the JSONP request there should be a function call
jQuery3110512…_1489476298510() {
}

Open in new window

(... would be expanded to whatever was in the URL)
Can you confirm that this is what you are getting back.

Author

Commented:
?callback=jQuery31105120590335855826_1489476298510&_=1489476298511

this is what i am getting
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Yes but what is the JSONP look like - if you expand the request and look at the response tab you should see the code that is returned.
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Author

Commented:
{"MT_Claims_Resp":{"BuyingPrice":"Value","CreatedBy":"Value","Currency":"Value","Description":"Value","Distributor":Value,"DistributorCity":"Value"}
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Ok but that is not a JSONP response

Expecting to see a function
jQuery31105120590335855826_1489476298510 ({"MT_Claims_Resp":{"BuyingPrice":"Value","CreatedBy":"Value","Currency":"Value","Description":"Value","Distributor":Value,"DistributorCity":"Value"} 
});

Open in new window

Author

Commented:
Can you please help me with how to consume the response

{"MT_Claims_Resp":{"BuyingPrice":"Value","CreatedBy":"Value","Currency":"Value","Description":"Value","Distributor":Value,"DistributorCity":"Value"}


.Really thanks that you are reply to my queries .

Author

Commented:
i am writing below code

 $.ajax({
    crossDomain: true,
    type: 'GET',
    username: "value",
   password: "value",
    contentType: "application/json; charset=utf-8",
    url: "value",
    dataType: "jsonp",
    success: onDataReceived
   
});

function onDataReceived(data)
{
debugger;
alert(JSON.stringify(data));
    alert("Data received");
   
}
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
As I mentioned before - if that i the response as you are getting it it is not JSONP but plain JSON
Also you don't need crossDomain - the actual use case for that parameter is when you are calling to the SAME domain but want jQuery to treat it as if it is a CROSS DOMAIN request.

Next - username and password are not valid ajax parameters - if you want to send username and password you have to include those in the data property.

contentType: "application/json; charset=utf-8" you use if you are SENDING JSON to the receiving service - otherwise you do a simple get request
I don't know anything about your service but I am going to guess you want something like this

$.ajax({
    type: 'GET',
    data: {
        username: "value",
        password: "value"
    },
    url: "value",
    dataType: "json"
}).done(function(resp) {
   console.log(resp);
   // resp should be a JavaScript object you can use directly
});

Open in new window

Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
If that does not work and this is a JSONP Request then you have to setup the function to receive the data

function jQuery31105120590335855826_1489476298510(resp) {
   console.log(resp);
   // access resp as you would a standard JavaScript object
}

Open in new window

Author

Commented:
Thanks for the help.The web service is an SAP Pi web service which i am trying to consume in share point online through ajax calls .The web service is accessible only in a specific domain
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Not sure if there was a question there - please advise where we are with this thread. Did the earlier posts solve the problem?

Author

Commented:
$.ajax({
    type: 'GET',
    data: {
        username: "value",
        password: "value"
    },
    url: "value",
    dataType: "jsonp"
}).done(function(resp) {
   console.log(resp);
   // resp should be a JavaScript object you can use directly
});

i am getting the reponse in the netwrok tab of the browser but still its not getting printed in the console .The "jQuery31105120590335855826_1489476298510" number always changes each time i run it so this might not be the function name i guess .I am using the above code
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Ok I think I understand what is going on - this should be enough for it to work
$.ajax({
    type: 'GET',
    data: {
        username: "value",
        password: "value"
    },
    url: "t2205.php",
    dataType: "jsonp"
}).done(function(resp) {
   console.log(resp);
   // resp should be a JavaScript object you can use directly
});

Open in new window


This should dump the response to the console.

The "jQuery31105120590335855826_1489476298510" number always changes each time i run it so this might not be the function name i guess .I am using the above code
Ignore my previous comment - you set the function name when you are handling the AJAX request yourself. jQuery creates that function name for you and the service creates the function call based on that callback parameter.

What concerns me though is that on your network call you are not seeing this

jQuery111108669811181329767_1489497411652({...});

Open in new window

Instead you are seeing just the returned structure. If this is a JSONP service you should see something like the above in the response tab of the network request.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial