'ivirabyan/jquery-mentions how to check ajax request respone
I am using ivirabyan/jquery-mentions in my project. i can do ajax request, but the problem is how can i use the response
$('textarea.mentions').mentionsInput({
source: function( request, response ) {
$.ajax({
url: rootPath() + "user/tagFriends/" + request.term,
type: "GET",
dataType: "json",
success: function(data){
alert(data);
// found data here
}
});
},
showAtCaret: true
});
Thanks in advance.
Solution 1:[1]
You can try this:
$.ajax({
url: rootUrl + '/your_controller/',
type: "GET",
contentType: 'application/json',
// YOUR DATA COMING FROM THE VIEW TO THE CONTROLLER (IF IT NEED IT).
data: "{ 'id':'" + id + "', 'user': '" +user+ "'}",
dataType: 'json',
success: function (result) {
if (result) {
//DO YOUR STUFF. FOR EXAMPLE. SHOWING A DIV
$('#your_div').append("<div>Hi there. Controller send this: "+data+"</div>");
// IF YOR DATA IS AN OBJECT. YOU CAN ACCESS DIRECTLY.
// data.attribute1, data.attribute2,...
// EQUALS FOR A LIST OF OBJECT AFTER LOOP IT.
} else {
//DO YOUR STUFF
}
},
failure: function (data) {
// DO YOR STUFF IN FAILURE CASE.
},
});
Solution 2:[2]
Don't use jQuery for the ajax part: it worked for me.
$('textarea.mentions').mentionsInput({
source: function(request, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
callback(JSON.parse(this.responseText));
} else {
callback([]);
}
}
}
xhr.open('GET', '/ajax.php?search=' + encodeURIComponent(request.term));
xhr.send();
},
showAtCaret: true
});
ajax.php :
$answer =
array(
array(
'value' => 'alex',
'uid' => 'user:1',
),
array(
'value' => 'andrew',
'uid' => 'user:2',
),
array(
'value' => 'vincent',
'uid' => 'user:3',
),
);
echo json_encode($answer);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Elias MP |
| Solution 2 | Tyler2P |
