I have a GET request:
$.ajax({
url: '/items?ids=' + value.join(','),
method: 'get',
dataType: 'json'
})
How can I create a POST request from this?
$.ajax({
url: 'url_here',
method: 'post',
data: {
fName1: value1,
fName2: value2,
...
},
dataType: 'json'
});
The way of post requests works are different from the get request, in http post request you need to pass the values in the body of your request as bellow :
$.ajax({
url: "/items",
contentType: "application/json",
type: "POST",
data: {
"id": $("#id").val(),
"value1": $("#value1").val(),
"value2": $("#value2").val()
}
});
you can use fiddler to debug and watch what going on the wire.
Related
I'm trying to use jQuery and Ajax to send a POST request to my API.
$.ajax({
url: '/api/Orders',
headers: {
contentType: "application/json"
},
type: "POST",
data: JSON.stringify({'description':'test'}),
});
When I'm using postman (chrome extension) the POST request is fine and everything works great.
If I'm trying to use the above code with AJAX the response is:
message:"Request must have "Content-Type: application/json" header"
I find it really weird because I set contentType : "application/json".
Did you have tried to set the property dataType to JSON?
Your code will looks like:
$.ajax({
url: '/api/Orders',
headers: {
contentType: "application/json"
},
type: "POST",
data: JSON.stringify({'description':'test'}),
dataType: "json"
});
After 6 hours i found the correct answer.
$.ajax({
url: 'http://127.0.0.1:5000/api/Orders',
headers: {
accepts: 'application/vnd.api+json'
},
contentType: "application/json",
type : 'post',
data: JSON.stringify({
'description' : 'test'
}),
dataType: "json",
success: function(data) {
console.log(data);
}
});
I'm trying to get a data from server without refreshing the page using ajax, So the problem the data come like a text not like json data
my code:
$.ajax({
type: "GET",
url: "http://localhost:8080/search?key=" + QUERY + "",
success: function (reslt) {
console.log(reslt);
console.log(reslt.length);
}
});
and the data on the server:
im using nodejs and express framework the code:
router.get('/search', function (req, res) {
tab = ['08:00', '09:00', '10:00', '11:00'];
res.end(JSON.stringify(tab));
});
why when i do console.log(reslt[3]); it's give me 8 , should give me 10:00
Use
dataType: 'json'
If your response is JSON, always set the datatype to json. Do it like this
$.ajax({
type: "GET",
dataType: 'json',
url: "http://localhost:8080/search?key=" + QUERY + "",
success: function (reslt) {
console.log(reslt);
console.log(reslt.length);
}
});
You have to use dataType and contentType attribute in your ajax request
$.ajax({
type: "GET",
dataType: 'json',
contentType: "application/json",
url: "http://localhost:8080/search?key=" + QUERY + "",
success: function (reslt) {
console.log(reslt);
console.log(reslt.length);
}
});
Hello I'm performing a GET request on a RESTful Rails resource, like so:
function getGroups(category) {
$.ajax({
type: 'GET',
url: 'http://localhost:3000/groups.json',
data: JSON.stringify({"access_token":"569669d8df0456", "category":category }),
success: function(data) {alert(data)},
contentType: "application/json",
dataType: 'json'
});
});
getGroups("own_groups");
The problem is that Webrick server errors out like this:
ERROR bad URI
`/groups.json?{%22access_token%22:%22569669d8df0456%22,%22category%22:%22own_groups%22}'.
It must be something related with how the JSON data is parsed, because I am having no problems with another GET request WITHOUT JSON data, and a POST request WITH JSON data...
Update: adding code for POST request (where JSON.stringify is required)
function addGroup(name, description) {
$.ajax({
type: 'POST',
url: 'http://localhost:3000/groups.json',
data: JSON.stringify({"access_token":"569669d8df0456", "group_name":name, "group_description":description}),
success: function(data) { alert("ok")},
contentType: "application/json",
dataType: 'json'
});
};
addGroup("nice group", "full of people");
Do not use JSON.stringify. Simply put:
data: {"access_token":"569669d8df0456", "category":category },
Moreover, you do not need to specify complete url http://localhost:3000/groups.json, it can be just simply groups.json
Below is my jquery ajax call. I see from fire bug that I am getting the json response.
Content-Type application/json
{x:1363590711.97,y:0.277528026651}
However...I cant event pop up and alert of the data? How do I get the parsed json object so i cna start working iwth it?
$.ajax({
type: 'GET',
url: 'ajax_test',
crossDomain: false,
dataType: 'json',
success: function(responseData) {
alert(responseData);
//seriesJsonData[0]['data'].push({y: responseData.y, x: responseData.x});
}
});
Your return data is already parsed when you request dataType: 'json'
$.ajax({
type: 'GET',
url: 'ajax_test',
crossDomain: false,
dataType: 'json',
success: function(responseData) {
alert(responseData.x + " " + responseData.y);
seriesJsonData[0]['data'].push(responseData);
}
});
So I want to make a http request with a body (say file, file2, file3) and arguments like action=delete how to make such request with jquery?
Pass parameters with url and files with body:
$.ajax({
url: 'someurl' + '?action=delete',
type: 'POST',
data: requestbodyhere,
success: function(responseData){
}
});
You want to do a POST? You can do that using Ajax:
jQuery.ajax({
type: 'POST',
url: url,
data: {
file1:'file1',
flie2:'file2'
},
success: function(response) {
//in response you have the answer from the server as a string
}
});