I have a JSON object that I'm getting as a response to an AJAX call:
{ "Score": 5, "OS": "Windows 7" }
I want to add it to a div but the following does not work, data.OS or data.Score just return as undefined
$.ajax({
type: "POST",
url: '/details',
data: JSON.stringify(IP),
contentType: 'application/json;charset=UTF-8',
success: function(data) {
$('#OSdetails').append('<div id="details">Operating System: ' + data.OS + '</div>');
}
});
What am I doing wrong?
$.ajax({
dataType: 'JSON', <==== THIS IS MISSING
type: "POST",
url: '/details',
data: JSON.stringify(IP),
contentType: 'application/json;charset=UTF-8',
success: function(data) {
dataType specifies the expected data type and allows for automated conversion
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);
});
}
});
This is in my default.aspx
$.ajax({
url: "Default.aspx/Myfunction",
dataType: "json",
type: "POST",
data: {someParameter: "some value"},
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.d);
},
error: function (d) {
alert("error");
}
});
And this is in my codebehind:
[WebMethod]
public static string Myfunction(string someParameter)
{
return "hello";
}
It keeps going to the error. I see that if I send the Ajax request with null for data and no parameters on the function I get the data "hello" out. So there is some issue in how I send the data, but it is unclear what
Put your parameters in quotes
$.ajax({
url: "Default.aspx/Myfunction",
dataType: "json",
type: "POST",
data: {'someParameter': 'some value'},
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.d);
},
error: function (d) {
alert("error");
}
});
Data object must be a single string.
data: JSON.stringify({someParameter: "some value"})
Change this line:
data: {someParameter: "some value"},
to:
data: {"someParameter": "some value"},
JSON object properties needs to be enclosed with quotes.
Here is how I do the same thing:
$.ajax({
type: "POST",
dataType: "json",
url: "RouteService.asmx/getRouteData",
data: { techID: techID, jobID: jobID },
success: function(msg) {
processRouteData(msg);
}
Try removing this line:
contentType: "application/json; charset=utf-8",
I have a post statement,
$.post("panel.php", 'data=[{"action":"UserInfo"}]', function (userInfo){
//processing
});
I need it to be converted to $.ajax so made it thus,
$.ajax({
type: "POST",
url: "panel.php",
data: { data: [{"action":"UserInfo"}]},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(userInfo) {
//processing
}
});
But the post variable isn't being sent. Is this not the correct way?
Can you try something like this:
$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
success: function(userInfo) {
//processing
}
});
Try this
$.ajax({
type: "POST",
url: "panel.php",
data: "action="+"UserInfo",
success: function(userInfo) {
//processing
}
});
Remove data from your data and keep it in a variable, stringify before your send as below
var data={"action":"UserInfo"};
$.ajax({
type: "POST",
url: "panel.php",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(userInfo) {
//processing
}
});
Your data attribute was not written as correct JSON:
data: { "data": [{"action":"UserInfo"}]},
You need quotation marks around the items inside your JSON object. You can use JSONLint to check if your JSON object is valid.
$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
dataType: "json",
success: function(userInfo) {
//processing
}
});
Need a small change. there is a predefined format to send data in ajax,
data: {status: status, name: name},
data: "status="+status+"&name="+name.
Follow any one of the approach.
try like this,
$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
success: function(userInfo) {
}
});
I am trying to make url shortener that uses goo.gl API. But i stucked when I have to get short URL from JSON response!
After entering this code in Chrome Console:
var longURL = "http://stackoverflow.com/questions/ask"
$.ajax({
url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{ longUrl: "' + longURL +'"}',
dataType: 'json',
success: function(response) {
var result = JSON.parse(response);
}
});
I get following ouptut:
I see that my short URL is in resoinseText.id. How to extract it from there?
You don't need to call JSON.parse(), because jQuery does that automatically when you specify dataType: 'json'. The value you want will then be in the id property of response.
var longURL = "http://stackoverflow.com/questions/ask"
$.ajax({
url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{ longUrl: "' + longURL +'"}',
dataType: 'json',
success: function(response) {
console.log(response.id);
}
});
Below is my jquery ajax call. I see from fire bug that I am getting the json response.
Content-Type application/json
{x:1363590711.97,y:0.277528026651}
However...I cant event pop up and alert of the data? How do I get the parsed json object so i cna start working iwth it?
$.ajax({
type: 'GET',
url: 'ajax_test',
crossDomain: false,
dataType: 'json',
success: function(responseData) {
alert(responseData);
//seriesJsonData[0]['data'].push({y: responseData.y, x: responseData.x});
}
});
Your return data is already parsed when you request dataType: 'json'
$.ajax({
type: 'GET',
url: 'ajax_test',
crossDomain: false,
dataType: 'json',
success: function(responseData) {
alert(responseData.x + " " + responseData.y);
seriesJsonData[0]['data'].push(responseData);
}
});