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
Shashank2235Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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.
0
Shashank2235Author Commented:
?callback=jQuery31105120590335855826_1489476298510&_=1489476298511

this is what i am getting
0
Julian HansenCommented:
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.
0
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

Shashank2235Author Commented:
{"MT_Claims_Resp":{"BuyingPrice":"Value","CreatedBy":"Value","Currency":"Value","Description":"Value","Distributor":Value,"DistributorCity":"Value"}
0
Julian HansenCommented:
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

0
Shashank2235Author 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 .
0
Shashank2235Author 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");
   
}
0
Julian HansenCommented:
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

0
Julian HansenCommented:
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

0
Shashank2235Author 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
0
Julian HansenCommented:
Not sure if there was a question there - please advise where we are with this thread. Did the earlier posts solve the problem?
0
Shashank2235Author 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
0
Julian HansenCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.