Avatar of Shashank2235
Shashank2235
 asked on

Getting unexpected token error while consuming web service in sharepoint

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
AJAXMicrosoft SharePoint

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon
Julian Hansen

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.
Shashank2235

ASKER
?callback=jQuery31105120590335855826_1489476298510&_=1489476298511

this is what i am getting
Julian Hansen

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Shashank2235

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

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

Shashank2235

ASKER
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 .
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Shashank2235

ASKER
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");
   
}
Julian Hansen

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

Julian Hansen

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Shashank2235

ASKER
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
Julian Hansen

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

ASKER
$.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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.