I wrote this code:
(function($) {
var url = 'http://surfujpametno.roditelji.me/2014/01/23/surfujpametno-aplikacija-za- android/?json=get_all_posts&callback=?';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'callback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.log(json.status);
console.log(json);
try{
json = $.parseJSON(json);
alert(json);
}catch(e){
alert('invalid');
}
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
http://surfujpametno.roditelji.me/2014/01/23/surfujpametno-aplikacija-za-android/?json=get_all_posts&callback=?
That is JSON url
I get 'Invalid'... If I remove try and catch i get this error: http://prntscr.com/2sf8bd
Try this, Removed $.parseJSON, because of dataType: 'jsonp', already defined
(function($) {
var url = 'http://surfujpametno.roditelji.me/2014/01/23/surfujpametno-aplikacija-za-android/?json=get_all_posts&callback=?';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'callback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.log(json.status);
console.log(json);
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);
As Paulloz indicated your json is already a JSON object: JSfiddle without parse and alert: http://jsfiddle.net/Skadi2k3/cKUD7/
Related
I have a json which is . I just want to get specific data which is
obj['contacts']['name']
How can i get
obj['contacts']['name']
name on Contacts array
This is my code:
$.ajax({
type: 'GET',
dataType: 'json',
url: uri,
cache: false,
contentType: 'application/json',
success: function(data) {
for (var obj in data) {
console.log(obj['contacts']['name']);
}
}
});
In your case this is how you want get name from contacts
$.ajax({
type: 'GET',
dataType: 'json',
url: uri,
cache: false,
contentType: 'application/json',
success: function(data) {
if (!data.contacts) return;
var names = data.contacts.map(function(dt) {
return dt.name;
});
console.log(names);
}
});
Just enumerate the returned object "contacts" property:
$.ajax({
type: 'GET',
dataType: 'json',
url: uri,
cache: false,
contentType: 'application/json',
success: function(data) {
data.contacts.forEach(function(contact) {
console.log(contact.name);
});
}
});
i got my json string inside the ajax as function like this way
$.ajax({
type: "POST",
url: "http://localhost/./Service/GetPageInfo",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
filename: filename
}),
success: function (data) {
alert('Success');
},
error: function () {
alert('Error');
}
});
here i get data like
[{"main":{"sub":[],"tittle":"manu","startvalue":"","stopvalue":"","status":"","accumalated":"","comment":""}}]
i want it in a variable like
var myjsonobject =[{"main":{"sub":[],"tittle":"manu","startvalue":"","stopvalue":"","status":"","accumalated":"","comment":""}}]
There you go :
$.ajax({
type: "POST",
url: "http://localhost/./Service/GetPageInfo",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
filename: filename
}),
success: function (data) {
alert('Success');
var jsonobject = data;
},
error: function () {
alert('Error');
}
});
Also I strongly advise you to use promises to make API calls: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise
var jsonobject= null;
$.ajax({
type: "POST",
url: "http://localhost/./Service/GetPageInfo",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
filename: filename
}),
success: function (data) {
jsonobject=data;
alert('Success');
},
error: function () {
alert('Error');
}
});
If you want wait for ajax response and fill up variable then pass async: false in ajax request options.
Based on your comment, you need to parse the JSON in your success handler,
success: function (data) {
alert('Success');
var myjsonobject = JSON.parse( data );
},
This code gets a json object from another server.
How do I access the response outside of the function?
(function($) {
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'callback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
var data = json;
}
})
})(jQuery);
You can do something like this!
_outerMethod: function(data) {
//...do what you need to do
}, (function($) {
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'callback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
_outerMethod(json);
}
})
})(jQuery);
Now I have that code:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29",
data: {},
dataType: "jsonp",
crossDomain: true,
success: function(data) {
console.log(data)
} });
});
and get this error:
Uncaught SyntaxError: Unexpected token :
I can see the right response from the url in my console and tried different dataTypes. What could be wrong?
Please try this. On my end this works.
$.ajax({
type: "POST",
url: "http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=StatTrak%E2%84%A2%20P250%20%7C%20Steel%20Disruption%20%28Factory%20New%29",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
// an example of using result
var myVar1 = result.lowest_price;
var myVar2 = result.median_price;
var myVar3 = result.success;
var myVar4 = result.volume;
alert(result.success);
},
error: function (result) {
},
fail: function (arg1, arg2, arg3) {
}
});
I am executing this code:
var element=null;
$.ajax({
type: 'GET',
async: false,
url: "C:\Users\myDir\Desktop\Project\jsonfile.json",
dataType: 'json',
success : function(data) {
element=data;
}
});
JSON structure:
{
"info":[
{
"a1": "Ram",
"b1": "P123"
},
{
"a1": "ROM",
"b1": "P245"
}
]
}
but i am getting nothing in variable
Check for any error in the ajax using
var element=null;
$.ajax({
type: 'GET',
async: false,
url: "jsonfile.json",//Edited
dataType: 'json',
success : function(data) {
element=data;
}
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
may be the permission issue for the file. insert the json file with all your other code files and give relative path in the url
$.ajax({
type: 'GET',
async: false,
url: "jsonfile.json",
dataType: 'json',
success : function(data) {
element=data;
}
});
//location of json file is same as the file making ajax call