ajax respsonse undefined object - ajax

Although there is many posts with similar question,i couldnt managed to resolve it.
I can see at Console of my browser my object returns ok as array.
when i alert data.AlliasName i get undefined.
my json returns
{"data": [{"id":1,"FirstName":"101","LastName":"101","AlliasName":"101","Address":"kentro","Type":"1"}]}
$.ajax({
url: urlreq,
dataType: "json",
success: handleData
});
function handleData(data) {
alert(data.AlliasName);
console.log(data);
//do some stuff
}
I ve tried also puting the alert into success, data[0].AlliasName and eval func,always same result.

The data being returned contains a property called "data", which is an array, so the way to access this would be:
function handleData(data) {
console.log(data.data[0].AlliasName);
}

Related

Problem with getting data of the ajax field by a request in k2 (joomla)

The task is to get the value of some field from k2 with an ajax request. I Googled that it seems like a simple task and is solved by referring to the desired method in the controller
$.ajax({
type: "POST",
url: 'index.php?option=com_k2&task=photo',
dataType: 'json',
success: function (json) {
console.log('success '+json);
},
error: function (jqXHR, text, error) {
console.log('error '+text+' '+error);
}
});
in folder components/com_k2/controllers/item.php
i create function - public function photo(){return 1;} http://joxi.ru/a2XxYpJFwxvRV2
when trying to get my "1" :) I get "File Not Found" as soon as I did not try to access the k2 controller, everything is without success.

.eq() comes back as 'not a function'

I am trying to get information from an ajax object. I have the following jquery function inside a .then() method, but I am getting an error that reads "data.items.eq is not a function." I have tried a combination of .eq() and .children(), but they don't seem to work.
$.ajax({
url: "https://www.googleapis.com/books/v1/volumes?q=" + userInput,
type: "GET",
}).then(
(data) => {
console.log(data);
$('#book-info').html(data.items.eq(0).volumeInfo.title);
}
![This is the ajax object that I am trying to get the title from.] (https://i.imgur.com/YmK6nZS.png)
Simply use data.items[0].volumeInfo.title instead. .eq() is a method of the jQuery object and will not exist for the JavaScript array data.items.

forge.request.ajax returns: "Invalid parameter not satisfying: url"

We have an ios application built with trigger.io. this application is using forge.request.ajax to send data to our servers. one of our requests occasionally throws an error and returns this:
{"message":"Invalid parameter not satisfying: url","type":"UNEXPECTED_FAILURE"}
since the input parameters are sent in json format I suspected that some characters inputted by users, could break the structure and cause this error. my code looks like this:
forge.request.ajax({
url: "someurl.php",
dataType: "json",
data:"some=data&and=some&more=data&which=is inputted by user",
success: function (data) {
},
error: function (error) {
forge.request.ajax({
url: "errorlog.php",
dataType: "json",
data:"data=" + encodeURIComponent(JSON.stringify(error)),
success: function (data) {
},
error: function (error) {
}
});
}
});
this code gives the above error half the time. and work on the other half. are there any limitations for input parameters in ajax request? since i can't edit objective-c code, i need a solution - preferably a filter- which ensures this function to work with whatever input is entered.
Using encodeURIComponent may help:
var data = "some=data&and=some&more=data&which=is inputted by user";
forge.request.ajax({
url: "someurl.php",
dataType: "json",
data: encodeURIComponent(data)
...
Passing request data as URL Parameters has more than it's share of gotchas though so it may also be worth taking a look at this StackOverflow question: When are you supposed to use escape instead of encodeURI / encodeURIComponent?

Return a a json object from an ajax call to a django view

Am trying to return a json object to render to a grid in my template.
This is how i do it.
views.py
def ajax_up(request):
history_data=Upload_history.objects.all()
history=serializers.serialize("json",history_data)
return HttpResponse( history, mimetype='application/json' )
html
$(".reply").click(function(){
$.ajax({
url: "/ajax_up/",
type: 'GET', //this is the default though, you don't actually need to always mention it
dataType: "json",
success: function(data) {
alert("awasome"+ data)
},
failure: function(data) {
alert('Got an error');
}
});
so i declare an object to hold the data as
var data = {{history|safe}};
where history is returned from the ajax call as in view above
but when i do alert(data), i get [object object],[object object].....
can any one help please?
Sounds like it's working, but alert just displays a string. Since your data is not a string, it'll show [object Object].
Either serialize the data with JSON.stringify or use console.log instead of alert to see the data in your browser javascript console.

jquery ajax data shows [object Object]

I have a very basic ajax call to alert the data that was reported from the server
$.ajax({
type: "POST",
url: "/someform/act", //edit utl to url
data: { changed: JSON.stringify(plainData) }, //edit to include
success: function(data) {
alert(data); //data not $data
},
error: function() {
//error condition code
}
});
According to the docs on the jquery website regarding data field on the success callback, it says that data returned is the data from the server. However for some strange reason when I alert $data, I get [object Object]
I was expecting to see something like this, since that is what the server would send back
<status>0</status>
EDIT:
data is also passed along as the POST
You need to use JSON.stringify(data) in the alert to get anything readable.
Also, $data is a completely different variable name than data.
alert() prints the string representation of the arguments - hence if you pass an object, you'll get [object Object].
To inspect data, use console.log(data) better.
If you server send a JSON, you need to put dataType: 'json' to your ajax call. Be aware there's some mistake in your ajax call.
$.ajax({
type: "POST",
url: "/someform/act", // NOT 'UTL',
data: {
key: value,
key2: value2
},
// or data: plaindata, // If 'plaindata' is an object.
dataType: 'json',
success: function(data) {
console.log(data); // As moonwave99 said
},
error: function() {
//error condition code
}
});
EDIT
When sending data, you should send an object. jQuery will handle the array to sned it to the server. So if plain data is an object, it should be like this
data: plainData,
If you're sending data via $.ajax({...}), the Network tab of your browser inspector might be showing [object Object] in the Payload (Chrome) / Request (Firefox) sub-tab, like in the following image (Firefox):
The reason for this might be in the way you're forming your AJAX call. Specifically:
$.ajax({
url: '/ajax/example-endpoint',
data: {'fooKey':fooData,'barKey':barData},
type: 'post',
cache: false,
contentType: false, // this one will turn your data into something like fooKey=fooData&barKey=barData
processData: false, // and this one will make it [object Object]:""
beforeSend: function() {
// whatever it is you need to do
},
success: function(data) {
// do stuff
},
error: function(desc, err) {
// do stuff
}
});
When combined, contentType: false and processData: false turn your data into [object Object], because you're actually telling your AJAX call to ignore the content type of whatever is being sent, and not to process it.
If it's IIS, try creating a site outside of the Default Web Site (for example localhost/ajax1). For example a new site ajax1, place it not in the DefaultAppPool, but in your pool, for example ajax1. Try http://ajax1

Resources