$.ajax post doesn't post data (JavaScript) - javascript

I can't figure out why this problem is arising. When I make an ajax post request in my javascript file, the url gets called, but the data wont get sent. Here's how I make a post request:
<script type="text/javascript">
$.ajax({
type: "POST",
url: "/test",
dataType: "json",
data: {test: "test"},
contentType: "application/x-www-form-urlencoded",
});
</script>
And on my backend, here's the post method I'm using:
app.post('/test', function(req, res) {
console.log(req.body); // returns {}
console.log(req.params); // returns {}
});
Here's what I tried so far: XMLHttpRequest to Post HTML Form, AJAX Post not sending form data , Send POST data using XMLHttpRequest but unfortunately none of them worked for me.

dataType: "json" expects data in JSON, but contentType: "application/x-www-form-urlencoded" sends data in different way. Maybe you should write dataType: "json", contentType: "application/json" or dataType: "html", contentType: "application/x-www-form-urlencoded"?

Related

Posting raw data to a WebAPI using JQuery and AJAX

I am trying to work out how to post raw data to a webAPI using JQuery and Ajax, but I cannot seem to get the data to be sent across.
The endpoint works in Postman:
Here is my rather simple JQuery:
$.ajax({type: "POST", url: myUrl, data: ["21924"]});
What am I missing? This is not my usual area so I've had a good look through other questions, but non seem to quite fit! How do I recreate this Postman call via JQuery and Ajax?
Thanks.
Try specifying the data type and stringifying your data, like this:
$.ajax({
type: "POST",
url: myUrl,
data: JSON.stringify(["21924"]),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){console.log(data);},
failure: function(errMsg) {console.log(errMsg);}
});

How to post data using POST method and in json format using ajax

I m new to this so please bear with me.
I want to call an API which accepts data in POST method and in json format.
Please tell me how to call ajax which will send data in post format and in json.
I have tried this :
$.ajax({
url: 'http://domain.com/api',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'POST',
data: formData,
success: function (response) {
}, error: function() {
alert("Error! Please try again.");
}
});
formData in the above code is in array format.
Kindly guide me.

send json data to another url - after you've got it back from my own url

This is how the json I get from my page send when I clicked post, I throw a different URL.
That piece of data should like to come to look like this:
api.quickpay.net/subscriptions?currency=dkk&order_id=testworld&description=MB: 6-testworld
But when I get my json back watching it like this:
{"Prices":220,"HiddenIdMedlemskab":6,"ordre_id":"6-8315042016","currency":"DKK","description":"MB: 6-8315042016"}
The json should I have shed into a url that I added earlier.
My jquery code looks like this:
$.ajax({
type: 'post',
url: '/Medlem/medlemskabet',
dataType: 'json',
data: JSON.stringify(requestData),
contentType: 'application/json; charset=utf-8',
async: true,
processData: false
});
Over on my url /Medlem/medlemskabet
That gets the dates that I need for my json to specify package price orderid etc
So my question is How do I tag my json about throwing it over to the api URL and throws it into their system.
I've built since the MVC C #
You can pass the JSON object as an argument to $.get(), and jQuery will convert the properties into URL parameters.
$.ajax({
type: 'post',
url: '/Medlem/medlemskabet',
dataType: 'json',
data: JSON.stringify(requestData),
contentType: 'application/json; charset=utf-8',
async: true,
processData: false,
success: function(response) {
$.get('api.quickpay.net/subscriptions', response);
}
});

Can I "namespace" an ajax POST request that uses FormData?

I found this answer explaining how to use a FormData object to upload a file over ajax. I'm wondering if it's possible to rearrange the data structure that's sent over in the POST request.
My backend is in rails, and the controller expects the data like this:
xmlFile: {
some_property: 1,
attachment: [the file]
}
However, if I use a solution similar to the one from the above link, something like
var data = new FormData();
data.append('some_property', id);
data.append('source', file);
(where file is a File object), and then submit that with $.ajax
$.ajax({
url: url,
data: data,
processData: false,
type: 'POST',
contentType: 'multipart/form-data',
mimeType: 'multipart/form-data'
});
the form submits, but the data isn't namespaced under xmlFile. If I try to do something like
$.ajax({
url: url,
data: {xmlFile: data},
processData: false,
type: 'POST',
contentType: 'multipart/form-data',
mimeType: 'multipart/form-data'
});
it doesn't work. The request data just shows [object Object].

Why is an ajax post with json content type sending data url encoded?

Why is this call showing the values encoded onto the URL like so?
http://localhost:49597/api/auth?user=jon&password=123
Am using the following ajax call...
$.ajax({ url: "api/auth",
type: "get",
data: { user: "jon", password: "123" },
dataType: "json",
contentType: "application/json; charset=utf-8"
});
I want the data to be sent as json...
Because it is a GET request.
GET will send data in the querystring. If you want to avoid that, you can change your type to POST POST will send the data in the request body.
$.ajax({ url: "api/auth",
type: "post",
//other stuff
});
If it is a Login form, you should probably use POST method.
GET requests do not have a request body, therefore all info must be stored in url as query parameters. I would also recommend not authenticating users with Javascript, and would definitely make that a POST request.
Because the code specifies a request of type GET. A GET request passes the parameters via a query string. You should switch to a post if you do not want to use a query string.
$.ajax({ url: "api/auth",
type: "post",
data: { user: "jon", password: "123" },
dataType: "json",
contentType: "application/json; charset=utf-8"
});

Categories

Resources