We help IT Professionals succeed at work.

Getting unexpected token error while consuming web service in sharepoint

Shashank2235
Shashank2235 asked
on
197 Views
Last Modified: 2018-02-06
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

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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.

Author

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

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");
   
}
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

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
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions