Typeahead.js Get ID - ajax

sorry but i don't find reply..
My problem is i can't retrieve the id of the selected product. The value = 0
My code :
$("#recherche").typeahead({
onSelect: function(item) {
alert(item.value); // = 0
},
ajax: {
url: "/personne/autocompletation",
displayField: "nomComplet",
triggerLength: 1,
method: "POST",
dataType: "JSON",
preDispatch: function (query) {
return {
query: query
}
}
},
});
My code HTML
<li class="active" data-value="0"><strong>C</strong>alloway</li>
Sorry for my english...

Try this
$("#recherche").typeahead({
source: function(query, process){
$.ajax({
url: "/personne/autocompletation",
displayField: "nomComplet",
triggerLength: 1,
method: "POST",
dataType: "JSON",
success: function (data) {
console.log(data);
}
});
}
});

Related

Kendo DataSource Update function Not Firing

I know this question has been asked with answers I have reviewed over and over. I still can not get this kendo dataSource update function to fire. It fires without the function(e) {}
update:
{
url: "/SettingsAdmin/UpdateGrid",
dataType: "json",
type: "POST",
//data: {
// result: kendo.stringify(result.data)
//},
},
That works. What doesn't work is when I try implementing the update as a function
update: function(e)
{
debugger
alert("update")
$.ajax({
url: "/SettingsAdmin/UpdateGrid",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
data: {
models: kendo.stringify(e.data.models)
},
success: function (result) {
alert(e.success(result));
},
error: function (result) {
alert(e.error(result))
e.error(result);
}
});
},
Here is the whole dataSource defined.
return new kendo.data.DataSource({
transport: {
read: {
url: url,
contentType: 'application/json; charset=utf-8',
type: "POST",
dataType: "json"
},
update:
{
url: "/SettingsAdmin/UpdateGrid",
dataType: "json",
type: "POST",
//data: {
// result: kendo.stringify(result.data)
//},
},
update: function(e)
{
debugger
alert("update")
$.ajax({
url: "/SettingsAdmin/UpdateGrid",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
data: {
models: kendo.stringify(e.data.models)
},
success: function (result) {
alert(e.success(result));
},
error: function (result) {
alert(e.error(result))
e.error(result);
}
});
},
parameterMap: function (data, operation) {
return JSON.stringify(data);
}
},
schema:
{
data: "result.data",
total: "result.total",
aggregates: "result.aggregates",
groups: "result.groups",
errors: "result.errors",
model:
{
id: "name",
//fields: {
// //id: { required: false, type: 'number', nullable: true },
// name: { editable: false, type: "string" },
// value: { editable: true, type: "string" }
//}
}
},
pageSize: 10,
batch: false,
serverPaging: true,
serverFiltering: true,
serverSorting: true,
});
All transport actions (read, update, create, destroy) must be defined in the same way which I wasn't doing. I had defined my read action as
read: {
url: url,
contentType: 'application/json; charset=utf-8',
type: "POST",
dataType: "json"
},
And then the update with a function
update: function(e)
{ }
Can't do that. I need to define my read: as a function

Mvc autocomplete ajax

Try to:
$(document).ready(function () {
$('#cityName').autocomplete({
source: function(request,response) {
$.ajax({
type: 'POST',
url: '#Url.Action("Search", "City")',
dataType: 'json',
data: { name: request.term } ,
success: function (data) {
response($.map(data, function (item) {
alert(JSON.stringify(data));
alert(JSON.stringify(item.name));
return {
name: item.name,
label: item.name
}
}));
}
})
},
messages: {
noResults: "", results: ""
}
})
})
In alert(JSON.stringify(data)) got this: {"items":["Boston","Berlin"]}.
In alert(JSON.stringify(item.name)) got this: undefined.
Question: how do it (item.name) works?
You have to just return array of strings:
$(document).ready(function () {
$('#cityName').autocomplete({
source: function(request,response) {
$.ajax({
type: 'POST',
url: '#Url.Action("Search", "City")',
dataType: 'json',
data: { name: request.term } ,
success: function (data) {
response(data.items);
}
})
},
messages: {
noResults: "", results: ""
}
})
})

Can not pass value from Ajax to controller

My AJAX:
$.ajax({
url: '{{ URL::to('dashboard') }}',
type: 'GET',
data: { cid: val },
dataType:'JSON',
success: function(result) {
},
error: function(){
$('#status-msg').addClass('alert alert-danger');
$('#status-msg').text('Fejl!!');
}
});
My Route:
My Controller:
public function dashboard(){
if (Request::ajax()){
$cid = Input::get('cid');
var_dump(json_encode($cid));
} else {
echo "XX";
}
}
The cid is not passing to controller, the request AJAX is not working.
have you try change your ajax type to POST ?
$.ajax({
url: '{{ URL::to("yourURL") }}',
type:'POST',
data:"cid="+$(this).val()+"&_token=" + $("input[name=_token]").val(),
dataType:'JSON',
success:function(result){
},
error: function() {
}
});
Edit:
try to pass _token if you use {{Form::open}}.
hope this will help.
Try this:
$.ajax({
url: '<?php echo base_url() ?>index.php/contorller_name/method_name',
type: 'POST',
data: { cid: val },
dataType:'JSON',
success: function(result) {},
error: function() {
$('#status-msg').addClass('alert alert-danger');
$('#status-msg').text('Fejl!!');
}
});

How can I get imgur.com album images using ajax request

I used this code :
(function($){
var albumID = 'NNbeO';
var albumAPI = "https://api.imgur.com/3/album/" + albumID + "/images";
$.ajax({
url: albumAPI,
headers:{
'Authorization':'xxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
},
type: 'GET',
dataType: 'json',
success: function(data) {
alert(data.data[0].link);
},
error: function() { console.log("ERRORZ"); }
});
})(jQuery);
But I got this error :
{
"data": {
"error": "Malformed auth header",
"request": "\/3\/album\/NNbeO\/images",
"method": "GET"
},
"success": false,
"status": 403
}
I got my solution. It works fine now. Below is my working code. It's problem was, I've not added Client-ID text with Authorization headers.:
(function($){
var albumID = 'NNbeO';
var albumAPI = "https://api.imgur.com/3/album/" + albumID + "/images";
$.ajax({
url: albumAPI,
headers:{
'Authorization':'Client-ID xxxxxxxxxxxxx'
},
type: 'GET',
dataType: 'json',
success: function(data) {
alert(data.data[0].link);
},
error: function() { console.log("ERRORZ"); }
});
})(jQuery);

How to get the ID property in KendoUI TreeView

Here is my tree view of KendoUI:
<script src="~/scripts/kendo.all.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var regions = {
type: "odata",
transport: {
read: function (options) {
$.ajax(
{
type: "POST",
url: "Territory/AllRegions?countryID=?" + options.ID,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
$("#treeview").data("kendoTreeView").dataSource.data(data);
}
});
},
},
schema: {
model: {
hasChildren: function () {
return false;
}
}
}
};
var countries = {
type: "odata",
schema: {
model: {
id: "Id",
hasChildren: "HasChildren",
children: regions
}
},
transport: {
read: function (options) {
$.ajax(
{
type: "POST",
url: "Territory/AllCountries?territoryID=?" + options.ID,
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
$("#treeview").data("kendoTreeView").dataSource.data(data);
}
});
}
}
};
var Territories = new kendo.data.HierarchicalDataSource({
type: "odata",
transport: {
read: function (options) {
$.ajax(
{
type: "POST",
url: "Territory/AllTerritories",
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
$("#treeview").data("kendoTreeView").dataSource.data(data);
}
});
}
},
schema: {
model: {
hasChildren: "HasChildren",
Id: "ID",
children: countries
}
}
});
$("#treeview").kendoTreeView({
dataSource: Territories,
dataTextField: ["Name", "Name", "Name"],
dataValueField:"ID"
});
});
</script>
The hierarchy is Territories->countries->regions
I can see the territories populating, but I am unable to fetch the ID property of selected Territory so that countries can be populated, that is in ajax call,
options.ID is undefined there.

Resources