jQuery AJAX polling for JSON response, handling based on AJAX result or JSON content
Date : March 29 2020, 07:55 AM
should help you out I'm a novice-to-intermediate JavaScript/jQuery programmer, so concrete/executable examples would be very much appreciated. , You could use a simple timeout to recursively call ajax_request. success: function(xhr_data) {
console.log(xhr_data);
if (xhr_data.status == 'pending') {
setTimeout(function() { ajax_request(); }, 15000); // wait 15 seconds than call ajax request again
} else {
success(xhr_data);
}
}
if (xhr_data.status == 'pending') {
if (cnt < 6) {
cnt++;
setTimeout(function() { ajax_request(); }, 15000); // wait 15 seconds than call ajax request again
}
}
|
split json array response for each input field
Date : March 29 2020, 07:55 AM
hop of those help? If the ajax request is of datatype: 'json' then the response will a json array $.each(data, function(idx, error){
$("#"+error.error+"_error").html(error.message);
})
|
Split AJAX response (JSON)
Date : March 29 2020, 07:55 AM
Hope this helps I would just use reduce and push items into an object that holds arrays. var items = [
{
title: "Product 1",
price: "12.00",
product_range: "range-1"
},
{
title: "Product 2",
price: "12.00",
product_range: "range-2"
},
{
title: "Product 3",
price: "12.00",
product_range: "range-3"
}
];
var grouped = items.reduce( function (obj, item) {
if (!obj[item.product_range]) obj[item.product_range] = [];
obj[item.product_range].push(item);
return obj;
}, {});
console.log(grouped);
console.log(grouped["range-1"]);
|
Ajax JSON response undefined in prompt field
Date : March 29 2020, 07:55 AM
will help you An ajax call is usually asynchronous. So your prompt is called before the ajax finished. Try: $.ajax({
url: "{{ url('/dashboard/popfieldexist') }}",
dataType: "json",
type: 'POST',
data: "_token={{ csrf_token() }}&"+"selector="+f1[0].getAttribute('data-file'),
success: function (response) {
if(response){
getPopfield = response.data.field;
}else{
getPopfield = " ";
}
var title = prompt("File name : ", ""+getPopfield+"");
}
});
|
How to output the json response from Laravel using Jquery ajax (when errors occur)
Date : March 29 2020, 07:55 AM
it fixes the issue According to the jQuery.ajax() document, the error callback accepts 3 parameters: error: function(jqXHR, status, error) {
var errorObj = jqXHR.responseJSON; // This only works when dataType is defined as "JSON", or the "Content-Type" of HTTP response is "application/json". Otherwise, use the below general code.
//var errorObj = JSON.parse(jqXHR.responseText);
alert(errorObj.errorMsg);
}
|