Reputation: 1144
I wrote a single common ajax function. Instead of writing multiple times. So I just sending post data and URL to my common function. Till here everything is fine and I'm getting a response too. But I'm getting trouble to read that response.
function _ajax_post(someData,URL)
{
var ajaxCall = $.ajax({
type: 'POST',
data: someData,
dataType: 'json',
url: URL
});
ajaxCall.done(function(data) {
console.log("A good programmer looks both ways before crossing a one-way street :) ");
});
ajaxCall.fail(function(data) {
console.log(" Design is choosing how you will fail.");
});
ajaxCall.always(function() {
console.log("Truth can only be found in one place: the code.");
});
return ajaxCall;
}
Here I calling like
$('#btnOutput').on('click', function () {
var str = $("#out").text();
var r = _ajax_post({"data":str},url+"roles/vu_menus/tets");
console.log(r);
});
In my console.log() I'm getting a response like
{readyState: 1, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
abort: ƒ (a)
always: ƒ ()
complete: ƒ ()
done: ƒ ()
error: ƒ ()
fail: ƒ ()
getAllResponseHeaders: ƒ ()
getResponseHeader: ƒ (a)
overrideMimeType: ƒ (a)
pipe: ƒ ()
progress: ƒ ()
promise: ƒ (a)
readyState: 4
responseJSON: {data: "[{"text":"Home","href":"http://home.com","icon":"f…"fas fa-filter","target":"_self","title":""}]}]}]"}
responseText: "{"data":"[{\"text\":\"Home\",\"href\":\"http:\/\/home.com\",\"icon\":\"fas fa-home\",\"target\":\"_top\",\"title\":\"My Home\"},{\"text\":\"Opcion2\",\"href\":\"\",\"icon\":\"fas fa-chart-bar\",\"target\":\"_self\",\"title\":\"\"},{\"text\":\"Opcion3\",\"href\":\"\",\"icon\":\"fas fa-bell\",\"target\":\"_self\",\"title\":\"\"},{\"text\":\"Opcion4\",\"href\":\"\",\"icon\":\"fas fa-crop\",\"target\":\"_self\",\"title\":\"\"},{\"text\":\"Opcion5\",\"href\":\"\",\"icon\":\"fas fa-flask\",\"target\":\"_self\",\"title\":\"\"},{\"text\":\"Opcion6\",\"href\":\"\",\"icon\":\"fas fa-map-marker\",\"target\":\"_self\",\"title\":\"\"},{\"text\":\"Opcion7\",\"href\":\"\",\"icon\":\"fas fa-search\",\"target\":\"_self\",\"title\":\"\",\"children\":[{\"text\":\"Opcion7-1\",\"href\":\"\",\"icon\":\"fas fa-plug\",\"target\":\"_self\",\"title\":\"\",\"children\":[{\"text\":\"Opcion7-1-1\",\"href\":\"\",\"icon\":\"fas fa-filter\",\"target\":\"_self\",\"title\":\"\"}]}]}]"}"
setRequestHeader: ƒ (a,b)
state: ƒ ()
status: 200
statusCode: ƒ (a)
statusText: "OK"
success: ƒ ()
then: ƒ ()
__proto__: Object
Now, How can I get responseText value?
Upvotes: 0
Views: 221
Reputation: 28611
As your ajax wrapper returns the ajax promise, you can use that in your click event handler in exactly the same way as you do in the wrapper:
function _ajax_post(someData,URL)
{
var ajaxCall = $.ajax({
type: 'POST',
data: someData,
dataType: 'json',
url: URL
});
ajaxCall.done(function(data) {
console.log("global done");
});
return ajaxCall;
}
$('#btnOutput').on('click', function () {
var str = $("#out").text();
var r = _ajax_post({"data":str}, url+"roles/vu_menus/tets");
// at this point, r is the ajax promise, so can use .done / .fail etc
r.done(function(data) {
console.log(data);
});
});
Upvotes: 1