Pass array from ajax to python (Tornado) - javascript

I have the following AJAX post.
I'm using the framework Tornado and I don't know how to send the information of the bidimensional array 'point' to the server in order to make some operations with the elements of the array, and then send the result back to the client.
$.ajax(
{
type: "POST",
url: "application/json",
data: {point:point},
cache: false,
contentType: false,
processData: false,
success: function (data, textStatus, response)
{
alert(data);
},
statusCode: {
401: function (response, textStatus, errorThrown)
{
alert("Error");
}
}
});
Thank you

In your get handler in tornado you can do either:
data = json.loads(urllib.unquote_plus(self.request.body))
or you can do this:
data = tornado.escape.json_decode(self.request.body)
Both of these will json decode the body of request in tornado

Related

jQuery.get with data creates url with [object%20object]

I am trying to make a GET request using jQuery. Instead of jquery appending my data name/vale pairs to the end of my url, it is appending [object%20object].
I am using both the $.ajax and $.get functions. See $.ajax and $.get
In researching this issue, I have found suggestions to add processData: false and contentType: false to my settings object (which shouldn't matter), but these options do not fix my problem.
Can someone confirm that this is a bug? Could my endpoint be blocking my request? But if so, why would that affect the url params?
here is my code that I am trying.
HTML
<input name='email'/> <button class='btn-submit'>Submit</button>
JS
$('.btn-submit').click(function(e) {
e.preventDefault();
var email = $('[name=email]').val();
var data = {
"apitoken": 'MYTOKEN',
"listid": 1111,
"email": email
};
var endpoint = "https://myendpoint.com/";
$.ajax({
url: endpoint,
method: 'GET',
processData: false,
contentType: false,
data: data,
success: function (data, status, jqxhr) {
console.log('success', data, status);
},
error: function(jqxhr, status) {
console.log('error', jqxhr, status);
}
});
// THIS IS THE GET METHOD
$.get(endpoint, data, function(data, status, jqxhr) { console.log('success', data, status); });

How to get each data before ajaxSend in JSON not after complete/success

I want all the ajax params before sending an ajax request in JSON format and I need to encrypt each value in JSON and again pass to the ajax request.
I get data in URI format as see in below code, not in JSON. How can I get that?
Around 200 Ajax in this format:
$.ajax({
type: "POST",
url: site_url + "user/user/login_action",
data: login_parms,
success: function (data) {
},
error: function (xhr, textStatus, errorThrown) {
}
});
Before Ajax Call:
$(document).ajaxSend(function(event, jqxhr, settings) {
console.log("settings :",settings.data);
});
Console log:
settings : vEmail=disha.c1%40grr.la&vPassword=123456789
Also if in AJAX use formData then how we can get each value of form data?
If you want to send a AJAX JSON CALL you must to use:
$.ajax({
type: "POST",
url: site_url + "user/user/login_action",
dataType: "json",
async: false,
contentType: "application/json",
data: JSON.stringify(login_parms),
success: function (data) {
},
error: function (xhr, textStatus, errorThrown) {
}
});
if you want to modify the param:
$.ajax({
beforeSend: function(xhr){
this.data
}
});

How to get rid from ?callback=myCallback&_=1340513330866?

I'm trying to capture data from an HTML page that is on the another website. I need to capture that data and save it into my site. That's why I used cross domain ajax like this
var myCallback = function(data) {
console.log(data);
};
var formData = $('.data-capture-form').serialize();
$.ajax({
url: 'http://prospectbank.co.uk/leads/capt',
type: 'GET',
data: formData,
dataType: 'jsonp',
crossDomain: true,
jsonp: 'callback',
jsonpCallback: 'myCallback'
}).done(function(res) {
console.log(res);
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR, textStatus, errorThrown);
});
Then I get this error
Any help? Thanks
The response from http://prospectbank.co.uk/leads/capt?_=1340513330866? is wrong formatted JSON.
{"status":true}
It should be
{status:true}

Internal Server Error when response text from ajax POST call is big

I am using an ajax call like the following:
$(function () {
$.ajax({
type: 'POST',
url: 'frmHistoryReportFinal.aspx/GetDataTable',
data: json_data,
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (responseText) {
if (responseText.d == "")
return;
parsedData = JSON.parse(responseText.d);
alert(parsedData);
count = 0;
},
error: function (xhr, textStatus, errorThrown) {
//process the error
alert(xhr);
alert(textStatus);
alert(errorThrown);
}
});
});
When I have a small set of data returned from my C# codebehind. The ajax call functions well. But when I have a json string with larger data, say 200 records returned then the ajax call gives error("Internal Server Error").
Please help me resolve the issue as I usually need to handle large datasets.

How to use $.ajax() for input field text AND FILE upload? [duplicate]

This question already has answers here:
jQuery AJAX file upload PHP
(5 answers)
Closed 8 years ago.
This is my code
$.ajax({
type: 'post',
url: '../lib/upload.php',
data: new FormData( $("#niceidentifier") ),
processData: false,
contentType: false,
success: function (response) {
if(response == 'success') {
return true;
}
else {
alert(response);
console.log(response);
}
}
});
The HTML form is just basic HTML (enctype included and method post) but unfortunately NO data is passed. How can I upload a file AND pass the input data once?
It's not as simple to pass files and other data, such as text, together in the same POST request. The only way to achieve that is making multipart/form-data request.
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
I use ajaxForm to upload files asynchronously via ajax, it is much easier than trying to implement your own.
http://malsup.com/jquery/form/
For a cross browser solution I think it is better to use
$("#form_id").ajaxSubmit();
You can use FormData to pass the File. and processData,contentType set to false compulsory. because you are not going to process file in client side.
// Create a formdata object and add the files
var data = new FormData();
$.each(files, function(key, value)
{
data.append('myFile', value); //No I18N
});
$.ajax({
url: '/your-url',
type: 'POST',
data: data,
cache: false,
dataType: 'json', //No I18N
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function(data, textStatus, jqXHR)
{
// do something
},
error: function(jqXHR, textStatus, errorThrown)
{
// Handle errors here
}
});
OR you can send file with data as following:
$( '#formId' )
.submit( function( e ) {
e.preventDefault();
$.ajax({
url: '/your-url',
type: 'POST',
data: new FormData( this ),
cache: false,
dataType: 'json', //No I18N
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function(data, textStatus, jqXHR)
{
// do something
},
error: function(jqXHR, textStatus, errorThrown)
{
// Handle errors here
}
});
});

Categories

Resources