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

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.

Related

ajax respsonse undefined object

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);
}

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.

Calling controller method (ASP MVC3) method from ajax doesn't work

I am using the following syntax to make a call to controller method from ASP page.
$.ajax({
url: 'ControllerName/MethodName',
type: 'POST',
contentType: 'application/json;',
data: JSON.stringify({ param: param1}),
success: function () {
alert("Success!!!");
},
error: function () {
alert("Failed!!!");
}
});
I have two ASP pages (views), both having same controller. If I call above method from first page, controller method gets called successfully. But if call same method from second page I get alert message "Failed". Also I tried using GET type, tried with other controller methods and all. Nothing will be called from second view. can anyone help me what can be problem? I am new to MVC.
Since your ajax is expecting result of JSON data from your Controller method do you have return Json(data, JsonRequestBehavior.AllowGet)?
Try change content type to:
contentType: 'application/json; charset=utf-8'
or/and specify url using mvc helper like:
url: #Url.Action("action"),
Works in my example. Hope it will help.

call ajax with mootools

Hi everyone I call ajax with mootols and obtain this error, I looking for internet but dont find anything
this is my code to call ajax
$$('.item-129 a').addEvent('click', function(event){
event.stop();
var req= new Request({
method: 'get',
url: '<?php echo JURI::root()?>index.php?option=com_content&view=article&id=6',
data: {'do': '1'},
onComplete: function(responseText){$('textos').set('html', responseText);}
}).send();
});
and this the error
TypeError: $("textos").set is not a function
anonymous()mootoo...ssed.js (lĂ­nea 959)
return self.apply(bind, args || arguments);
any idea!!!
it means that either:
the element is not found
you don't use mootools 1.2+, hence no set method, unlikely given you use Request and not ajax
jQuery has the $ or something else, which does not have the .set method
try first of all:
onComplete: function(){
console.log($("textos"));
}
if this responds, see what it returns. if you use jquery, it will return the wrapped jquery function. if it's mootools, it will be an element.
if you have jquery, mootools will silently drop to document.id instead.
so. write as document.id('textos').set('html', responseText)
if with document.id('textos') you don't get an element either, it's not found.

jQuery ajax - blank response returned

I am trying to use jQuery ajax to get some values from the database and then return them in an array.
I have used the same code several times before but this time, no response is being returned. Although the post values are the correct values that I would expect. Here is the javascript code that I am using:
$.ajax({ url: '/BlogArchive.asmx/ChangePost'
, type: 'POST'
, contentType: 'application/json; charset=utf-8'
, data: '{FileName:"' + FileName + '"}'
, dataType: 'json'
, success: function (data)
{
var arrayList = data.d;
var BlogPostTitle = $(".BlogPostTitle")[0];
var BlogPostDate = $(".BlogPostDate")[0];
var BlogPostContent = $(".BlogPostContent")[0];
$(BlogPostTitle).html(arrayList[0]);
$(BlogPostDate).html(arrayList[1]);
$(BlogPostContent).html(arrayList[2]);
}
// , error: function (XMLHttpRequest, textStatus, errorThrown)
// {
// //There was an error
// alert('dfd');
// }
});
The only javascript error that I am receiving is that data is null, which I would expect as the response is blank.
It seems that the name of the web method that I am calling from my javascript is not even being read, because if I changed 'ChangePost' to 'ChangePost1' for example, it still returns a blank response, although I would expect an error message saying that the web method can't be found.
It seems that it does recognise that the BlogArchive.asmx web service exists because if I put something that would create an error in the VB code, the error appears as the response.
I am sure this must be something simple that I am doing wrong. Any help would be appreciated.
, data: '{FileName:"' + FileName + '"}'
Seems odd. You probably meant:
, data: {FileName: FileName}
(or 'FileName=' + FileName)
Furthermore, did you inspect the request (and response) via FireBug or similar?
You should try using jQuery getJSON with the minimal arguments.
Another thing, when you are using JSON with jQuery, if the answer data are not wellformed
(like a space before / after the starting JSON string) could lead to a blank answer from
jQuery.
Be sure using traditionnal AJAX with jQuery that your answered data are correct.

Resources