jQuery syntax error for a valid json - javascript

I am trying to get the instagram user details using jQuery ajax which can be found from the url
https://www.instagram.com/instagram/?__a=1
But when i call the same through ajax from my following code
$(document).ready(function(){
var instagram = 'instagram';
$.ajax({
url : 'https://www.instagram.com/'+ instagram +'/?__a=1',
dataType: "jsonp",
success: function(e) {
document.write(JSON.stringify(e));
}
});
});
It is returning me a error :
missing ; before statement [In Json]
However when i copy and paste it for validation on some online tools available it is showing valid formatted json.
DEMO : https://codepen.io/abhibagul/pen/WXGoYb?editors=0010
Error is visible in the browser console.

If you are getting respone as JSON,
Change from dataType: "jsonp" to dataType: "json"
Valide json response would look like
{
"Name": "Foo",
"Id": 192.168.73.96,
"Rank": 7
}
And jsonp would be like
parseResponse({"Name": "Foo", "Id": 1234, "Rank": 7});
Get more details form here

Instagram does not support jsonp, it is not used much anymore in favor of CORS. Change jsonp to json. However, this will not work because Instagram's CORS policy prevents it. You will need to use the API provided by Instagram.
$(document).ready(function() {
var instagram = 'instagram';
$.ajax({
url: 'https://www.instagram.com/' + instagram + '/?__a=1',
dataType: "json",
success: function(e) {
document.write(JSON.stringify(e));
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Related

Jquery: Probably a syntax Issue in ajax() method - Value not getting sent

I'm able to dump value of the variable message in console .
But im not able to send it off in POST Request.
AJAX call:
chat.throwmsg = function(message) {
if ($.trim(message).length != 0) {
console.log(message);
$.ajax({
url: 'ajax/chat.php',
type: 'post',
data: { method: 'throw', message: message} ,
success: function(data) {
chat.fetchmsgs();
$('textarea#entry').val('');
}
});
}
}
This maybe due to wrong syntax, but I've tried both single and double quotes, and combination as well .
With a wild assumption, you are not having any error messages in developer console and chat.php has a post handler forthese parameters
Since your data is JSON, please change the code as this way and have a try..
var temp={ method: 'throw', message: message};
var param=JSON.stringify(temp);
$.ajax({
url: 'ajax/chat.php',
type: 'post',
data: param ,
dataType: "json",
success: function(data) {
chat.fetchmsgs();
$('textarea#entry').val('');
}
});
after reviewing the code I could not find any issues that restrict the data to be sent along with ajax request,if you are having any syntax errors you should have been warned prior the request initialization, however I recommend you to check the request header in the network tab of browser console and see your sending data along with the request if it's there you probably need to check the code of getting the post data in your server-side implementations

Ajax request failing?

I am trying to retrieve json data from an api.
It keeps failing, but when I look in the Net tab of Firebug I can see that the GET request executed and returned the correct data. Am I doing something wrong or does anyone have tips on how to debug this?
Edit: Have changed to dataType json and error status code is 0
Thanks
$.ajax({
url: 'http://localhost:55894/api/Test/All',
data: {
format: 'json'
},
error: function () {
alert('Error');
},
dataType: 'jsonp',
success: function (data) {
alert('Ok');
},
type: 'GET'
});
From the info you provided the reason it is failing is because you dont have a cross domain access policy setup. Because you are using different ports to host the website and the API you are running into this issue. You can either setup a crossdomain.xml with the proper security settings or you can move both the API and the webserver to the same port.
Have a look at this for more info: http://en.wikipedia.org/wiki/Same-origin_policy
u can try this way:
$.ajax({
type: 'GET',
url: 'url api here',
beforeSend: function() {
},
success: function(data) {
},
error: function(xhr) { // if error occured
},
complete: function() {
},
dataType: 'json'
});
JSON and JSONP are different. If you are using JSONP, the server side must be prepared to support it. Doesn't look like you are using JSONP.
So just change dataType to 'json', as you are "trying to retrieve json data".

Uncaught SyntaxError: Unexpected token : ajax call

So I'm trying to query the below json feed, however I keep getting the error in the topic.
I've searched around this site for possible answers however none that I've come across have worked so far. Commented out datatype and jsonp, jsonpCallback isnt it either, either is data, I've made sure that it validats via http://jsonformatter.curiousconcept.com/ and it does. I really dont know.
$.ajax({
type: 'GET',
url: 'http://raidbots.com/json/playerdata/us/mannoroth/usiris',
cache:true,
dataType: 'jsonp',
data: {
format: 'json',
},
success: ranks,
jsonpCallback:'callbackName',
error: function(data) { console.log(data); },
jsonp: false,
});
function callbackName(data){
console.log("jsonpCallback");
}
var ranks = function(data) {
console.log(data);
}
Thank you
-Art
The error is in your JSONp data because it's just JSON and not JSONp. JSONp requires the document to be valid JavaScript containing a function call.
If they don't support jsonp you need to use a proxy script (e.g. a php script on your server that retrieves the document) or ask them to send CORS headers so you can use a normal non-JSONp AJAX call to retrieve the data directly.

Extract and read JSON Data from web API

What I'm working on is providing 1 line instant definitions of terms and perhaps one line answers to few logical questions. Suppose a user inputs "JavaScript" and JavaScript visits the url https://api.duckduckgo.com/?q=JavaScript&format=json&pretty=1, gets the item "Definition" (Look at the API link to understand, Definition is in the first line itself) and displays its value of Definition and alert the user with the required data.
Anyways my code currently is:
<html>
<head>
<title>Hi</title></head>
<body>
<input id="ddgAPI"><button>Search</button>
<div id="output"></div>
</body>
</html>
Please note that I've not put in the required JavaScript/jQuery code as I'm confused with this. Thank you :)
Because this is a cross-domain request you can only do this with a proxy or with JSONP. Fortunately DuckDuckGo supports JSONP, so you just need to ensure that you add a callback parameter to the URL request like:
https://api.duckduckgo.com/?q=JavaScript&format=json&pretty=1&callback=jsonp
... or use the appropriate jsonp parameter with jQuery's ajax method, something like:
$('#ddgAPI').on('keyup', function(e) {
if (e.which === '13') {
$.ajax({
type: 'GET',
url: 'https://api.duckduckgo.com/',
data: { q: $(this).val(), format: 'json', pretty: 1 },
jsonpCallback: 'jsonp',
dataType: 'jsonp'
}).then(function (data) {
console.log(data);
});
}
});
Use jQuery.ajax() to talk to the remote service. url should be https://api.duckduckgo.com. type should be GET. data should be:
var data = { q:'JavaScript', format:'json', pretty:1 };
jQuery will then compile everything into an AJAX request, send it to the server. Pass a function as success so you can do something with the result:
$.ajax({
url: "https://api.duckduckgo.com",
type: "GET",
data: { q:'JavaScript', format:'json', pretty:1 },
success: function(data) { $('#output').html(data); }
});

How to process TFL news feeds?

Can anyone suggest an idea of accessing tfl traffic news feeds?. The following is the link to get the url for the data feed.
http://www.tfl.gov.uk/businessandpartners/syndication/16492.aspx
The code I tried is as follows:
Code Sample:
$jq.ajax({
type: 'GET',
url: 'http://www.tfl.gov.uk/tfl/businessandpartners/syndication/feed.aspx?email=***********#****.com&feedId=13',
dataType: "xml",
async: "false",
success: function(xml) {
$jq(xml).find('rr_event').each(function() {
//var title=$jq(this).find('title').text();
alert("success!");
});
}
});​
It looks like the link requires you to have valid credentials to access what I expect to be some sort of xml feed? Also, if you are trying to access this url from a different domain other than www.tfl.gov.uk, you will need to use JSONP to fetch the data otherwise you will run into a security exception (cross-domain error).

Categories

Resources