How to set the multiple same name query in javascript ajax - javascript

I have python api which can get the multiple data in same query such as.
createdata?ob=120&st=cool&st=warm
It can get st as ["cool","warm"] in python.
st = request.query_params.getlist("st")
print(st) #["cool","warm"] get the data as array.
Now I want to do the same thing from ajax
I try this
$.ajax({
type: "POST",
url: "defapp/createdata",
method: "GET",
data: {"ob":130,"st":"warm","st":"cool"},
contentType: "application/json",
and this.
$.ajax({
type: "POST",
url: "defapp/createdata",
method: "GET",
data: {"ob":130,"st":["warm","cool"]},
contentType: "application/json",
However former one get ["cool"] as st
later one get [] as st.

It's don't work,because you api not recive a data body,but it recive a params value.
You can changed to these below.
$.ajax({
type: "POST",
url: "defapp/createdata?ob=120&st=cool&st=warm",
method: "GET",
data: {},
contentType: "application/json",
I hope is can work in your case.

Related

How send data from js to flask

i have issue to send data in flask from js. I try use ajax.
AJAX code
$.ajax({
type: "GET",
contentType: "application/json;charset=utf-8",
url: "/getmethod/data",
traditional: "true",
data: JSON.stringify({data}),
dataType: "json"
});
Python code
def get_javascript_data(data):
content = request.get_data('data')
jsonify(content)
If u got some tips or tricks please tell me.
new_item = $('#input').val() //value I want to send
$.ajax({
url: '/set_value',
type: 'POST',
data: new_item,
success: function(response){
$('#main').text(response)
}
})
in flask:
new_item = request.get_data()

How to pass a single string in AJAX [without key]

I am trying to pass a single quoted string in data without key.
I write a code below :
$.ajax({
type: "POST",
url: 'MY_API_URL',
data: "HATETH",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
console.log(r);
}
});
it not works..
Please help me to do this. Is it possible or not?

AJAX POST request using JQuery with body and URL parameters

I'm learning how to make AJAX calls with JQuery and one thing I was wondering if it's possible to do is include some data as URL parameters and other data in the post body. For example, I'd like to do something like this:
$.ajax({
url: '/myURL',
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8'
})
but in addition to the JSON data which is being sent in the POST request body, I'd like to include URL parameters. Is there a way to do this?
You would be able to include the variables in the url portion of your code. For example
var example1 = "some_information";
$.ajax({
url: '/myURL',
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8'
})
would become
var example1 = "some_information";
$.ajax({
url: '/myURL?var1=example1',
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8'
})
You may need to put quotes around the example1 variable to ensure it doesn't break when there are spaces in the url.
You can send parameters normally in the URL as in the get request
either using ? and &
$.ajax({
url: '/myURL?data2=' + data2 + '&data3=' + data3,
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8'
})
or by set them between /
$.ajax({
url: '/myURL/' + data2 + '/' + data3,
type: 'POST',
data: JSON.stringify(data),
contentType: 'application/json; charset=utf-8'
})
The way of including the parameters on the URL will depends of the way you receive/parse them on the server side, however this is not considered as good practice for the post request.

apply encodeuricomponent to all ajax calls in an application globally

I have ajax calls like this at several places in my application.
$.ajax({
type: 'POST',
url: url,
data: Json.stringify(Values),
dataType: 'json'
});
For these ones, I would like to add encodeURIComponent to data sent as below:
$.ajax({
type: 'POST',
url: url,
data: encodeURIComponent(Json.stringify(Values)),
dataType: 'json'
});
Is there any way that I can do this globally without manually editing it everywhere?
Create your own function for doing it.
var myAjax = function (options) {
if (typeof options.data !== "undefined") {
options.data = encodeURIComponent(options.data);
}
return $.ajax(options);
};
Then in your code replace:
$.ajax({ type: 'POST', url: url, data: Json.stringify(Values), dataType: 'json' });
With:
myAjax({ type: 'POST', url: url, data: Json.stringify(Values), dataType: 'json' });
Whatever you do, don't monkey patch!

jquery Post , data object

I try to understand one thing.
I want to post an object with jquery Ajax POST , something like this:
var dataPostYear = {
viewType:GetViewType(),
viewDate:'2009/09/08',
languageId:GetLanguageId()
};
$.ajax({
type: "POST",
url: url,
data: dataPostYear,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnLoadYearListSuccess,
error: OnLoadYearListError
});
and it doesn't work.
But this one works fine:
var dataPostYear = "{viewType:'"+ GetViewType() + "',viewDate:'2009/09/08',languageId:'"+GetLanguageId()+"}";
$.ajax({
type: "POST",
url: url,
data: dataPostYear,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnLoadYearListSuccess,
error: OnLoadYearListError
});
GetViewType() return --'0'
languageId() return --'1'
it's just a string
there is a way to post an object, something what I try to do in my first way ? Or not ?
Thanks
Use jQuery.param(). Here is the documentation
You should look at .postJSON.
Essentially, you just add json as a 4th argument to the $.post
From the site:
// Send the request
$.post('script.php', data, function(response) {
// Do something with the request
}, 'json');
If you want the .ajax call version, you can convert it using the .post docs.

Categories

Resources