Second ajax call confuses url with object
Second ajax call confuses url with object
I've got a search box on my page that updates data in a datatable. Here is the code:
var baseURL = "/api/Reservations?CustomerId=";
$("#btnSearch").on("click",
function () {
var custEmail = $("#txtEmail").val();
var customer = $.ajax(
{
url: "/api/Customers?email=" + custEmail,
data: "",
success: function (customer) {
var ajaxurl = baseURL + customer.id;
var ajaxobj = {
url: ajaxurl,
dataSrc: ""
};
dt.ajax.url(ajaxobj).load();
dt.clear().draw();
},
error: function () {
bootbox.alert("Doesn't look like you've made any bookings.");
}
});
});
When I load the page, enter an email address in my searchbox and click the button, everything works perfectly. It searches the customer, retrieves the bookings and populates the table.
The request it makes to the reservations API looks like this:
http://localhost:55601/api/Reservations?CustomerId=6&_=1522178504857
(I'm not sure how or why &_=1522178504857 gets appended, but it doesn't seem to be affecting it so I'll come back to that...)
However, if I click the search button a second time, the customer is still retrieved correctly, however request to the reservations API looks like this:
http://localhost:55601/Reservations/[object%20Object]?_=1522178504858
(URLs in both cases I've retrieved from developer tools).
Just to show what happens:
Why? What have I done wrong?
EDIT: updated code formatting using Markdown
Answers
You have this in the AJAX call:
var ajaxurl = baseURL + customer.id;
Not sure where that is coming from but you also have this:
var customer = $.ajax(
I suspect the second time
customer
is the ajax call or something you are not expecting it to be.Kevin
Thanks Kevin, the purpose of the search is to look up a customer ID by the supplied email address (which works every time), then use that ID to return all reservations for that customer ID (which works the first time, then fails all subseuqent times).
Given that the customer is returned correctly every time, I'm not sure how the issue happens on all occassions after the first run.
I missed the
customer
parameter in this line:success: function (customer) {
I would start by adding
console.log(customer);
as the first line of yoursuccess
function. What does it show?Kevin
added consol.log(customer) as you suggest, it shows the same thing every time:
I've added some more logging, and this is what I've found:
On the first click, the url is an object which has a dataSrc property and a url property. On the second click it's a string.
I'm just not sure why it would change from one click to the next, given that the code is the same?
That is strange, not sure without trying it out. However the
ajax.url()
expects a string parameter. Maybe try removing the object and just using a string.Kevin
Thanks Kevin, I did initially have it as a string but got an undefined error. So changed it to an object and then it worked, but only the rist time it runs after loading the page.
Just to further confuse things...I was playing with the code, changing between passing
dt.ajax.url()
the object and the string. Now it logs without the child object every time in the console (including the first itme it runs, which still works):This makes sense, it's what I'd expect to see. Happy to write off the appearance of the child object as a non-repropducable anomoly as it doesn't seem to have affected the outcome.
Just in case it helps, here is where I initialise my table:
I've managed to figure out what the problem is. Normally, when you initialise your table, you specify an ajax object:
In my case, because I don't have the data until the user executes the search, I haven't specified this. Therefore, when I first run the ajax call for the datatable (not for the customer search), I have to pass it an object. Passing it the URL only results in an undefined error.
However, on subsequent runs, as the ajax object is already defined at this point, passing an object as the URL parameter has the expected effect - the URL is an object hence the
[object%20Object]
we are seeing.I had tried using the destroy() function previously to no effect. So, my workaround is simply to use an iteration counter. On the first run I pass it the object, on subsequent runs I pass it the URL.
I'm not sure if there's a better way of doing this.