Jquery Autocomplete not showing results [closed] - ajax

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have the following code:
var acOptions = {
source:function (request, response) {
$.ajax({
url: "index.php?option=com_fmw&view=keywords_api&controller=keywords_api&format=raw",
type: "GET", dataType: "json",
data: { expr: request.term},
sucess: function (data) {
response($.map(data, function (item) {
return item.value;
}))
}
})
},
minChars: 1,
dataType: 'json'
};
$( "#search_box_input" ).autocomplete(acOptions);
I get the following response from the server:
[{"value":"Greater"},{"value":"great"},{"value":"greatly"},{"value":"Greater-Axe"}]
However, the autocomplete field is not showing results, even though I can see that the ajax request got sent and that the server answered. What am I doing wrong?

sucess is spelt wrong. Try success instead.
success: function (data) {
response($.map(data, function (item) {
return item.value;
}))
}

Related

How to retrieve data received from AJAX call. Got Undefined

Having tough times trying to deal with data (see markersMethod). Got undefined. All declared logs work fine. In order to clarify the problem, I
provide some code from 3 files related to the ajax call.
This question is NOT a duplicate because the previous was about making ajax call(I figured it out by myself) then it was closed. The actual question deals with handling already made ajax call in the controller side. As you see, these questions are completely different although they share some parts of code.
markers.ejs file
function onMapClick(e) {
coords = e.latlng;
marker = L.marker(coords);
marker.addTo(map);
foo(coords)
}
function foo(coord){
$.ajax({
url:'/markers',
type: 'POST',
dataType: 'json',
data: {coordinates: JSON.stringify(coord)},
success: function(data, text, jqXHR) {
alert(data);
},
error: function(req, status, error) {
alert(status, error);
}
});
}
markersControllers.js file
markersMethod: (req, res) => {
data = req.data;
console.log(req)
console.log('req received');
main.js file
router.post('/markers', markersController.markersMethod)

Issues while making a POST to a Web API from JQuery [duplicate]

This question already has an answer here:
Calling Web Api POST always receives null value from jQuery
(1 answer)
Closed 3 years ago.
I have a web api with the following POST Method
public HttpResponseMessage Post([FromBody]string package)
I have a console app that uses the HttpCLient with no problems. When I try to make a call by means of jQuery, I get null on the package variable.
This is the code I have right now:
$.ajax({
url: 'http://localhost:8081/api/Package/',
type: 'POST',
data: JSON.stringify(message),
contentType: "application/json;charset=utf-8",
success: function (data) {
alert(data.length);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Status: '+xhr.status+', Error Thrown: '+thrownError);
}
});
The "message" variable is a complex model containing two properties.
What could I be doing wrong?
I'll appreciate your help...
There might be an issue with binding the value to the response sent to the server
try sending the data as
$.ajax({
url: 'http://localhost:8081/api/Package/',
type: 'POST',
data: { package : JSON.stringify(message) }
datatype: 'json',
success: function (data) {
alert(data.length);
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Status: '+xhr.status+', Error Thrown: '+thrownError);
}
});
I am assuming that your JSON.stringify(message) returns a string value
updated
public HttpResponseMessage Post([ModelBinder]string package)
allowing package to bind from everywhere instead of just body did it for me.

MVC + controller view not displaying [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am using ajax for posting 2 values to my controller, the controller got the values but wouldn't display the view. Please assist what I am doing wrong here. Thanks.
Javascript:
function grantPermission() {
window.FB.login(function (response) {
if (response.authResponse) {
uid = response.authResponse.userID;
accesstoken = response.authResponse.accessToken;
var postData = { facebookUID: uid, facebookAccessTok: accesstoken };
$.ajax({
url: '#Url.Action("SavePhoto")',
data: postData,
dataType: 'json',
success: function (response) {
// process the results from the controller action
// window.location.href = response.Url;
}
});
} else {
console.log('User cancelled login or did not fully authorize.');
alert('User cancelled login');
}
}, { scope: 'publish_stream' });
};
Button:
<input type="button" id="auth-loginlink" value="Proceed" onclick="grantPermission();"/>
Controller:
public PartialViewResult SavePhoto(string facebookUID, string facebookAccessTok)
{
return PartialView("BlurredPhoto");
}
You need to embed the returned HTML back into your document somewhere. e.g. If you add a
<div id='MyDiv'>
then change your ajax call to
success: function (responseText, textStatus, XMLHttpRequest) {
$("#MyDiv").html(responseText);
}
As Slaks has mentioned, you also need to change your return datatype accepts to 'html'.
You need to do something with the HTML source from the server in your success callback.
You should probably start by not telling jQuery that the HTML is datatype: 'json'.

How to pass the ajax post request from one server to another server using jquery with php? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pass data from jQuery to PHP for an ajax post
How do i pass the values from one server to another server using ajax,jquery
$.ajax({
url:'/url/on/other_server',
type: 'POST',
contentType:"application/x-www-form-urlencoded",
data: '{"key":"value"}',
success: function(data, status) {
console.log(status);
},
error: function(xhr, desc, error) {
console.log(error);
}
});
Ref: http://api.jquery.com/jQuery.ajax/

MVC3 Controller returning JsonFile [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I am having a problem with a json result. When calling from the jquery it is returning a file to be saved instead of executing the success function. The get jquery request occurs in the document.ready function.
Any help would be appreciated.
public ActionResult Locations()
{
LocationsModel lm = new LocationsModel();
return Json(lm.getPins(), JsonRequestBehavior.AllowGet);
}
I have also tried:
public JsonResult Locations()
{
LocationsModel lm = new LocationsModel();
return Json(lm.getPins(), JsonRequestBehavior.AllowGet);
}
The jquery is as follows:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: this.href,
data: "{}",
dataType: "json",
success: function (msg) { getPins_success(msg); },
error: OnError
});
Thanks,
Chris
Edit:
Never mind it was a duh. Once I moved the json request to another action in the controller and loaded the view it all worked out. Now I am having parsing problems but that is another issue all together.
you should use getJson instead.
For you it would be:
$.getJSON(this.href, function (msg) { getPins_success(msg); });
This will let you parse the return data as json.

Resources