Sending >1 GB arraybuffer using ajax to make a string file - ajax

I want to send a large arraybuffer using ajax to make a string file into local folder in my computer.
When im trying to send request using chrome console:
> var arr = new Uint8Array(999999999);
> arr[0]=[84]; // this is TEST TEXT
> arr[1]=[69];
> arr[2]=[83];
> arr[3]=[84];
> arr[4]=[32];
> arr[5]=[84];
> arr[6]=[69];
> arr[7]=[88];
> arr[8]=[84];
function stringFromArray(data)
{
var count = data.length;
var str = "";
for(var index = 0; index < count; index += 1)
str += String.fromCharCode(data[index]);
return str;
}
$.ajax({
url: 'http://localhost:19885/binaryfile.bin' ,
cache: false,
async: false,
type: 'POST',
data: '{Type: "File", content: "'+ stringFromArray(arr)+'"}',
dataType: "json",
processData: false,
});
My browser crashed and i couldt create any file.
Any help?

Related

What is the correct way of reading from 2 endpoints and displaying it in 1 table using ajax?

I have 2 tables(endpoints) - users and leaderboard. In users table their are columns user_id and full_name. In leaderboard table there is a user_id column and other columns. I want to display leaderboard table. But instead of user_id I want to show full_name column.
So I will have to take the full_name from the users table. This is my code:
1st ajax call -
var users = [];
$.ajax({
url: url + "admin/users",
type: "GET",
dataType: 'json',
async: false,
crossDomain: true,
success: function (result){
for (i=0; i < result.data.length; i++) {
users[i] = result.data[i];
}
}
})
2nd ajax call -
$.ajax({
url: url + "admin/leaderboard",
type: "GET",
dataType: 'json',
async: false,
crossDomain: true,
success: function (result){
for (i=0; i < result.data.length; i++) {
var row = document.getElementById("table-body").insertRow(i);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
for (j=0 ; j < users.length ; j++) {
if (result.data[i].user_id === users[j]._id) {
cell1.innerHTML = users[j].full_name;
break;
}
else {
cell1.innerHTML = 'NA';
}
}
cell2.innerHTML = result.data[i].points;
}
$('#dataTable').DataTable();
}
})
The 2nd ajax call occurs only after the 1st ajax call is completed.
Right now I have only 15 rows in the users table. But later on there will be thousands of rows. If there are thousands of rows in the users endpoint then the 1st ajax call will take a long time to complete. It will have detrimental effects to the end user's experience. So what would be the correct way of coding it?
The code is working fine. The only issue is the speed.

how to read response from ajax

I have a form and it reads another js file which has ajax. and then the formauthentication file in php.
now the response
is
"hasError":true,"errors":{"lastname":"lastname</b> is required.","firstname":"firstname</b> is required.","email":"e-mail</b> is required
notice the hasError is true.
now i am reading this through google chrome. how do i read this value on the webpage itself.
i want to use if hasError is true then do something. but i cannot and don't want to touch the ajax js file but in this page itself.
ajax code in a js file code
$.ajax({
type: 'POST',
headers: { "cache-control": "no-cache" },
url: callingFile + '?rand=' + new Date().getTime(),
async: false,
cache: false,
dataType : "json",
data: 'ajax=true&'+params+'&token=' + static_token ,
success: function(jsonData)
{
if (jsonData.hasError)
{
var tmp = '';
var i = 0;
for(var error in jsonData.errors)
//IE6 bug fix
if(error !== 'indexOf')
{
i = i+1;
tmp += '<li>'+jsonData.errors[error]+'</li>';
}
tmp += '</ol>';
var errors = '<b>'+txtThereis+' '+i+' '+txtErrors+':</b><ol>'+tmp;
$('#opc_account_errors').slideUp('fast', function(){
$(this).html(errors).slideDown('slow', function(){
$.scrollTo('#opc_account_errors', 800);
});
});
}
this is the ajax and this goes to an authentication.php file and returns a response
and works accurately as you have seen
hasError is true

building a list and sending to an AJAX method

I am building a list from an array that I then pass via AJAX using the data parameter.
Everything looks good when I write out my list to the console, but my AJAX method doesn't like it.
for (i = 0; i < carList.length; i++) {
var carData = { "data": { "CarId": '"' + CarList[i] + '"', "PassengerCar": "true", "Automatic":"true" } };
console.log(carData);
}
$.ajax({
dataType: "json",
data: carData
....
Am I doing something improper?
Thanks
var carData = {};
for (i = 0; i < carList.length; i++) {
carData["data_"+i] = {
CarId : CarList[i],
PassengerCar: true,
Automatic : true
};
}
$.ajax({
url : 'someurl.php',
dataType: 'json', //expects returned data from server to be JSON
data : carData
}).done(function(data) {
console.log(data);
});

MVC3 Ajax displayin all values in array

I get my values from my action control but i wanna show all my values. How to use for loop or foreach in ajax to show array all of elements ?
Here is my script
$.ajax({
type: "get", url: "Home/Oku", data: {}, dataType: "json",
success: function (data) {
for (var i = 0; i< data.length; i++) {
$("img#myimage").attr("src", data[i])
i++;
}
It is not very clear what you are trying to do. It is not clear what format your controller action uses to return the data. From the code you have shown I can assume that it returns an array of strings. And then I suppose that those strings represent urls to some images because you seem to be attempting to assign them to the src attribute of an img tag. Except that you are using an id selector and always overwriting the same img tag for each element.
If you want to have multiple images, then you could create them dynamically. Start by creating an empty placeholder:
<div id="images"></div>
and then:
$.ajax({
url: 'Home/Oku',
type: 'GET',
success: function (data) {
var images = '';
for (var i = 0; i < data.length; i++) {
images = images + '<div><img src="' + data[i] + '" /></div>';
}
$('#images').html(images);
}
});
This assumes that your controller action returns a JSON encoded array of strings representing urls for the images:
public ActionResult Oku()
{
var imageUrls = new[]
{
"http://example.com/foo.jpg",
"http://example.com/bar.jpg"
};
return Json(imageUrls, JsonRequestBehavior.AllowGet);
}

JqGrid checkbox, Controller retrieve null instead of int

I have a jqgrid with a checkbox column. when checkbox is pressed i need to update database.
I can retrieve id from my jqgrid, but when I'm sending it further to my controller I retrieve id = null
here is the code in load complete function
var iCol = getColumnIndexByName($(this), 'Aktiv'), rows = this.rows, i,
c = rows.length;
for (i = 0; i < c; i += 1) {
$(rows[i].cells[iCol]).click(function(e) {
var id = $(e.target).closest('tr')[0].id,
isChecked = $(e.target).is(':checked');
alert('clicked on the checkbox in the row with id=' + id +
'\nNow the checkbox is ' +
(isChecked ? 'checked' : 'not checked'));
$.ajax({
mtype: "POST",
url: '#Url.Action("Action")',
data: { actionparameter: id },
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
cache: false
});
;
if alert shows not null for the id you should verify your server code. Depend on your configuration you could need to use JSON.stringify method from json2.js like
data: JSON.stringify({actionparameter: id})
or just do no JSON specific prameters in the $.ajax call.

Resources