Link to home
Start Free TrialLog in
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
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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

ASKER

?callback=jQuery31105120590335855826_1489476298510&_=1489476298511

this is what i am getting
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.
{"MT_Claims_Resp":{"BuyingPrice":"Value","CreatedBy":"Value","Currency":"Value","Description":"Value","Distributor":Value,"DistributorCity":"Value"}
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

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 .
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");
   
}
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

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

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
Not sure if there was a question there - please advise where we are with this thread. Did the earlier posts solve the problem?
$.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
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial