I'm trying to do a jquery GET and i want to send a parameter.
here's my function:
$(function() {
var availableProductNames;
$.get("manageproducts.do?option=1", function(data){
availableProductNames = data.split(",");;
alert(availableProductNames);
$("#nameInput").autocomplete({
source: availableProductNames
});
});
});
This doesn't seem to work; i get a null in my servlet when i use request.getParameter("option");
If i type the link into the browser http://www.myite.com/manageproducts.do?option=1 it works perfectly.
I also tried:
$.get(
"manageproducts.do?",
{option: "1"},
function(data){}
which doesn't work either.
Can you please help me?
EDIT:
also tried
$.ajax({
type: "GET",
url: "manageproducts.do",
data: "option=1",
success: function(msg){
availableProductNames = msg.split(",");
alert(availableProductNames);
$("#nameInput").autocomplete({
source: availableProductNames
});
}
});
Still getting the same result.
If you say that it works with accessing directly manageproducts.do?option=1 in the browser then it should work with:
$.get('manageproducts.do', { option: '1' }, function(data) {
...
});
as it would send the same GET request.
Try this:
$.ajax({
type: 'get',
url: 'manageproducts.do',
data: 'option=1',
success: function(data) {
availableProductNames = data.split(",");
alert(availableProductNames);
}
});
Also You have a few errors in your sample code, not sure if that was causing the error or it was just a typo upon entering the question.
I got this working : -
$.get('api.php', 'client=mikescafe', function(data) {
...
});
It sends via get the string ?client=mikescafe
then collect this variable in api.php, and use it in your mysql statement.
This is what worked for me:
$.get({
method: 'GET',
url: 'api.php',
headers: {
'Content-Type': 'application/json',
},
// query parameters go under "data" as an Object
data: {
client: 'mikescafe'
}
});
will make a REST/AJAX call - > GET http://localhost:3000/api.php?client=mikescafe
Good Luck.
Related
I am new to using Ajax to get WordPress data.
The following code should return the server time but the response always is "400 Bad Request".
$.ajax({
url: obj + "?action=wps_get_time&format=U",
success: function(data) {
console.log(data);
}
});
Also tried it as POST and it was the same.
$.ajax({
url: obj,
method: "post",
data: { action: "wps_get_time", format: "U" },
success: function(data) {
console.log(data);
}
});
Any suggestions what's wrong please? Can't figure.
I always thought there are actions I can use always such as wps_get_time, without using a plugin. Am I wrong?
Ist there any easy way to get the server time by ajax?
Thank you all in advance.
The code below will return server time in Indochina and log it to console.
$.ajax({
type: 'GET',
cache: false,
url: location.href,
complete: function (req, textStatus) {
var dateString = req.getResponseHeader('Date');
if (dateString.indexOf('GMT') === -1) {
dateString += ' GMT';
}
var date = new Date(dateString);
console.log(date);
}
});```
I have a function that sends values to the db, the function works great until I include the success function with a location.reload(). Then it sends empty values.. sometimes though... sometimes it works fine, but I should be able to rely on it.
Any suggestions?
$.ajax({
url: 'core/manage_articles.php',
type: 'POST',
data: {
blog:title,
title:title,
text:text,
lat:lat,
lng:lng,
success: function() {
location.reload();
}
}
});
Well... you didn't say what lib you are using so I'll just go on a semiwild guess (might not be right):
$.ajax({
url: 'core/manage_articles.php',
type: 'POST',
data: {
blog:title,
title:title,
text:text,
lat:lat,
lng:lng
},
success: function() {
location.reload();
}
}
});
Try it like this.
I am using Ajax to add contents on my database. And here's the code:
function addToFavorites(){
var recipe_id = $("#recipe_id").val();
var url = connect_url+'addFavorite.php?user_id='+user_id+'&recipe_id='+recipe_id;
$.ajax({
type: 'POST',
data: [{
user_id: user_id,
recipe_id: recipe_id
}],
url: url,
async: true,
jsonpCallback: 'userCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function (data) {
alert("HELLO!!!");
},
error: function (e) {
alert("ERROR!");
}
});
}
The Ajax call was successful and I was able to add records on the database but I'm just wondering why is it not displaying the alert message if the calling was successful? Is there something wrong with my code? Or is there something wrong with my understanding? Thanks!
you must give a response with some info to the ajax or it won't know the response succeeded
Here is my JS code:
<script>
$("#comments").click(function(event) {
$.ajax({
type: "GET",
url: '/localhost:8080/comment',
data: JSON.stringify(
{
'name': 'anon',
'subject': 'MY COMMENTS',
}),
contentType: 'application/json',
success: function(data,textStatus, jqXHR) {
console.log('POST response: ');
console.log(data);
}
});
});
</script>
and here is my Python Code:
class Guestbook(webapp2.RequestHandler):
def get(self):
Jguest_data = json.loads(self.request.body)
return self.response.out.write(json.dumps(Jguest_data))
I got the error 404 Resource not found. After digging around there is some issues with localhost . so I tried with JSONP as follows:
<script>
$("#comments").click(function(event) {
$.ajax({ // ajax call starts
url: "localhost:8080/comment", //
type: "GET",
data: JSON.stringify(
{
'name': 'anon',
'subject': 'MY COMMENTS',
}),
dataType: "jsonp", // Choosing a JSON datatype
success: function(data,textStatus,jqXHR)
{
console.log('POST response: ');
console.log(data);
}
});
});
</script>
That still does not work... I get "No JSON object could be decoded" Error.
I tried replacing JSON.loads with JSON.load... and that still errors out...
Can someone please let me know what the issue is?
Thanks a mil in advance
when you do a GET request with ajax you are using url parameters.
so there is no body in the request.
make a POST and change your get() to post()
You should edit your app.yaml file if there is one in your project, and if there is not one, add one.
Add this code to your file (app.yaml).
-url: /comment
script: <url-to-the-server-side-script>
You can see a more complete app.yaml documentation for use with python here.
https://cloud.google.com/appengine/docs/python/config/appref
I did this lots of times:
var url = '/offers/1/voting';
var params = { 'direction': 'up' };
$.post(url, params, function() {
alert('callback');
}); // post
(I'm hardcoding the values for this example, but nothing)
So, through firebug I receive the desired JSON response (200 status), but the callback doesn't execute. It's pretty much the only javascript I'm using. Tried with jquery 1.6.4 and 1.7.1 and it's the same thing with both. I don't know what I'm missing.
Help me, Stack Overflow. You're my only hope.
If you use $.ajax instead of $.post ($.post is really an overwrite of $.ajax with fewer parameters), you can add a handler for error and see if it fires:
jQuery.ajax({
type: "POST",
async: true,
url: '/offers/1/voting',
data: { 'direction': 'up' },
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (msg)
{ alert('success') },
error: function (err)
{ alert(err.responseText)}
});
try this
$.post(url, params, function(callback) { alert(callback); });