jsgrid returns blank line after update - jsgrid

After giving an update via ajax in jsgrid the line returns blank
controller: {
loadData: function(filter) {
var data = $.Deferred();
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "/app/Play/",
dataType: "json"
}).done(function(response) {
data.resolve(response);
});
return data.promise();
},
updateItem: function(item) {
return $.ajax({
type: "POST",
url: "/app/Play/change.php",
data: item,
});
},
}
In the return of the url update: /app/Play/change.php
I return the updated record data in a normal json, same as the initial json used in the load

Resolved:
controller: {
loadData: function(filter) {
var data = $.Deferred();
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "/app/Jogos/app/",
data: filter, // <--Insert line, this is a problem
dataType: "json"
}).done(function(response) {
data.resolve(response);
});
return data.promise();
},
updateItem: function(item) {
return $.ajax({
type: "POST",
url: "/app/Jogos/app/change.php",
data: item
});
},
},

Related

Prevent Ajax form from being submitted twice?

Hello looking for some help with my form here.
How would I prevent " $(document).on('submit'..." getting triggered again when submitting it with " $(active_form)[0].submit();"?
Thanks!
$(document).on('submit', active_form, function(e) {
e.preventDefault();
active_form = $('div.account-address-form.visible form');
var vat_input = $('div.account-address-form.visible form input.vat_reg_no');
var but_prim = $('div.account-address-form.visible form .button-primary');
if (vat_input.val()) {
var url = "/apps/vat-check/";
var number = vat_input.val();
$.ajax({
type: "GET",
dataType: "jsonp",
url: url,
data: {
number: number
},
beforeSend: function() {
but_prim.prop('disabled', true);
but_prim.css('opacity', '.4');
},
success: function(data) {
var result = data.result;
if (result) {
$.ajax({
type: "GET",
dataType: "jsonp",
url: "/apps/customer-vat/",
data: {
email: "{{ customer.email }}",
vat_no: number,
update: true
},
success: function(data) {
$(active_form)[0].submit();
$(active_form).off("submit").submit();
$(document).on('submit', active_form, function(e) {
e.preventDefault();
if(!active_form.attr('validated'))
{
active_form = $('div.account-address-form.visible form');
var vat_input = $('div.account-address-form.visible form input.vat_reg_no');
var but_prim = $('div.account-address-form.visible form .button-primary');
if (vat_input.val()) {
var url = "/apps/vat-check/";
var number = vat_input.val();
$.ajax({
type: "GET",
dataType: "jsonp",
url: url,
data: {
number: number
},
beforeSend: function() {
// disabling button
but_prim.prop('disabled', true);
// set button
but_prim.css('opacity', '.4');
},
success: function(data) {
active_form.attr('validated',true);
var result = data.result;
if (result) {
$.ajax({
type: "GET",
dataType: "jsonp",
url: "/apps/customer-vat/",
data: {
email: "{{ customer.email }}",
vat_no: number,
update: true
},
success: function(data) {
active_form.attr('validated',true);
$(active_form)[0].submit();

ajax POST int parameter in asp.net core

I am migrating my MVC project to Core and I have been having a hard time fixing all the old ajax calls.
I can pass a model and string parameters into the controller, however, ints are not working for me.
I can wrap them into a JSON object as a string parameter such as [FromBody]string objId in the controller, but then I have to parse the int val from the Json {'objId' : 1}.
Is there a way I can avoid this and just pass an int?
below is the code I am trying.
[HttpPost]
public JsonResult PassIntFromView([FromBody]int objId)
{
//DO stuff with int here
}
here is the js.
var data = { "objId": 1};
$.ajax({
url: '#Url.Action("PassIntFromView", "ControllerName")',
data: JSON.stringify(data),
type: "POST",
dataType: 'JSON',
contentType: "application/json",
success: function(data) {
//do stuff with json result
},
error: function(passParams) {
console.log("Error is " + passParams);
}
});
The objId is always 0 in the controller.
I have tried this without doing JSON.stringify(data) as well with no result.
I have also tried all the different form attribute variations.
Try to use contentType as 'application/x-www-form-urlencoded':
var data = { objId: 1 };
$.ajax({
url: '#Url.Action("PassIntFromView", "ControllerName")',
type: "post",
contentType: 'application/x-www-form-urlencoded',
data: data,
success: function (result) {
console.log(result);
}
});
Then remove the [FromBody] attribute in the controller
[HttpPost]
public JsonResult PassIntFromView(int objId)
{
//Do stuff with int here
}
I believe your issue could be that you are passing an object to the api, but trying to turn it into a primitive. I know there is already a chosen answer, but give this a whirl.
var data = { };
data["objId"] = 1; //I just wanted to show you how you can add values to a json object
$.ajax({
url: '#Url.Action("PassIntFromView", "ControllerName")',
data: JSON.stringify(data),
type: "POST",
dataType: 'JSON',
contentType: "application/json",
success: function(data) {
//do stuff with json result
},
error: function(passParams) {
console.log("Error is " + passParams);
}
});
You create a model class
public class MyModel {
public int ObjId {get;set;}
}
Your controller should expect one of these
[HttpPost]
public JsonResult PassIntFromView([FromBody] MyModel data)
{
//DO stuff with int here
}
JSON has a preference for strings not integers. You are better off to use JSON.stringify(data) to parse to your controller, convert that to a integer in the controller, then parse the string that was returned as:
var data = { objId: 1};
$.ajax({
url: '#Url.Action("PassIntFromView", "ControllerName")',//asp.net - url: 'api/controllerName/controllerFunction'
data: JSON.stringify(data),
type: "POST",
dataType: 'JSON',
contentType: "application/json",
success: function(data) {
var result = JSON.parse(data);
//do stuff with json result
},
error: function(passParams) {
console.log("Error is " + passParams);
}
});
Try this:
var data = { "objId": 1};
$.ajax({
url: '#Url.Action("PassIntFromView", "ControllerName")',
data: data,
type: "POST",
dataType: 'JSON',
contentType: "application/json",
success: function(data) {
//do stuff with json result
},
error: function(passParams) {
console.log("Error is " + passParams);
}
});
Your controller:
[HttpPost]
public JsonResult PassIntFromView(int objId)
{
//DO stuff with int here
}
Js
var data = { "objId": 1};
$.ajax({
url: "ControllerName/PassIntFromView",
data: data,
type: "POST",
dataType: 'JSON',
success: function(data.result!=null) {
console.log(data.result);
},
error: function(passParams) {
console.log("Error is " + passParams);
}
});
I got it working like this
let numberFour = JSON.stringify(4);
$.ajax({
.......
.......
data: numberFour,
type: "POST",
dataType: 'JSON',
contentType: "application/json",
......
........
});

Uploading file using AJAX request

I'm trying to upload a file using AJAX. I tried to do it using formData() object as well. How can I add a file to following Ajax request?
$.ajax({
url: '/api/add_new_pair/',
method: "POST",
type: 'POST',
FILES: {
'image' : file,
},
data: {
'csrfmiddlewaretoken': csrftoken,
'text': text,
},
}).done(function(response){
console.log(response);
}
You can access the multipart(multimedia) data(header along with data in base64 format) in request.FILES and other data in request.POST
var data = new FormData();
data.append('csrfmiddlewaretoken', '{{csrf_token}}');
data.append('image', $('#image-id').files[0]);
$.ajax({
type: "POST",
url: "/save_design/",
contentType: false,
data: data,
async: true,
cache: false,
processData: false,
beforeSend: function() {
},
success: function(t) {
},
complete: function() {
},
error: function(t) {}
}),
}

how to get the json data from the url in jquery?

How to get the json array from the URL.I declare the mydata as a variable.how to get the json array into the mydata? using the following ajax
var mydata;
$.ajax({
url: someurl,
dataType: 'json',
success: function() {
}
});
var mydata;
$.ajax({
url: someurl,
dataType: 'json',
success: function(data) {
mydata = data;
console.log(mydata);
}
});
alternatively you could use $.getJSON() function
Try this:-
***$.ajax({
url: someurl,
dataType: 'json',
success: function(mydata) {
foreach(var data in mydata){
//do something.....
}
},
failure: function()
{
//error message
}
});***

jquery tooltip Getting undefined value

I am getting value through post method when i am add it into variable it is showing the same value but when i accessing the value from the outside. it showing me undefined.
var Trends =this.$elm.data('title.poshytip');
var value;
var path = window.location.pathname;
var name = path.substring(path.lastIndexOf('/') + 1);
var dataToSend='{ID:"'+Trends+'"}';
$.ajax({
type: "POST",
url: "UnitedStatesTrends.aspx/serverside",
data: dataToSend,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function( data )
{
value = data.d;
},
error: function() {
alert('error');
}
})
//here
alert(value);
// here i am getting value=undefined in alert but my value is 'UnitedStates'
this.$inner.append(
typeof content == 'function' ?
content.call(this.$elm[0], function(newContent) {
self.update(newContent);
}) :
//here i am setting the value to tooltip
content == '[title]' ? value : content
);
Well I think, you are forgetting to put a semicolan ; after this
$.ajax({
type: "POST",
url: "UnitedStatesTrends.aspx/serverside",
data: dataToSend,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function( data )
{
value = data.d;
},
error: function() {
alert('error');
}
});

Resources