jquery.ajax: additional argument to callback - ajax

how can I make mydata available in the success function below
function addWordAddress(mydata) {
var url = 'http://site/';
$.ajax({
url: url,
jsonpCallback: callbackName,
jsonp: false,
cache: true,
dataType: "jsonp",
success: function (json) {
console.log(json);
console.log(mydata);
},
error: function () {
console.log("error");
}
});
}

Related

jsgrid returns blank line after update

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
});
},
},

ASP.NET MVC AJAX POST ValidateAntiForgeryToken

With my code i get a internal Server error:
#using (Html.BeginForm("", "Manage", FormMethod.Post, new { role = "form"}))
{
#Html.AntiForgeryToken()
}
<script>
function Like(id) {
var requestData = {
profileId: id,
__RequestVerificationToken: $('[name=__RequestVerificationToken]').val(),
};
$.ajax({
url: '/Manage/Like',
type: 'POST',
data: JSON.stringify(requestData),
dataType: "json",
contentType: 'application/json; charset=utf-8',
error: function (xhr) { alert('Error: ' + xhr.statusText); },
success: function (result) {},
async: true,
processData: false
});
}
</script>
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Like(string profileId)
{ ... }
If i remove [ValidateAntiForgeryToken] everything works fine, but i lose the sercurity. How can i fix the internal server error?
I see in fiddler SyntaxView 2 requests:
/Manage/Like
{"profileId":13,"__RequestVerificationToken":"jH2ofYlcTiHl8lixW_ANEHOg5KgwRh5Xl43lQfGkDFh55jX-x5cmz4RfPtbDfu92oQsTM7zsop83ldfbxMdIIELYZ0kfByFcXjUp-5mwyKZcQzjXP2gy6qW0iQOtLsqaDjFSzoxnyqM2MD42CbItzw2"}
/Manage
__RequestVerificationToken=MNiKOJHZg7BGaTNccOjrR2Obf_nPhKfcwIPZVBUl53G368n5euzB4y1htH47VKg3V3mHfxkjYZDz6iPepQ7jpeXGARtlj6vV74B8zQbp4by9JR4Rcz4sHANm3WHb6WAXaLcsnFvWJth_8c98XKda5w2
Taking this from a sim. question here include antiforgerytoken in ajax post ASP.NET MVC
function Like(id) {
var form = $('form');
var token = $('input[name="__RequestVerificationToken"]', form).val();
$.ajax({
url: '/Manage/Like',
type: 'POST',
data: { __RequestVerificationToken: token, profileID: id },
error: function (xhr) { alert('Error: ' + xhr.statusText); },
success: function (result) {},
async: true,
processData: false
});
}

Error in clearGridData function

In version 4.8.0 free Grid
When call updatedate() I receive the next error:
TypeError: p is undefined
and the problem is at line
jQuery("#100").jqGrid('clearGridData',true);
function updatedate(){
dela=$("#listdela").val();
panala=$("#listpanala").val();
jQuery("#100").jqGrid('clearGridData',true);
$.ajax({
type: "POST",
dataType: "json",
url: "<?php echo JS_MODUL_PATH?>s_100.php",
data: "q=6&dela="+dela+"&panala="+panala,
success: function (json) {
jQuery("#100").jqGrid('setGridParam',{datatype:'json',loadui:'enable',url:"s_100.php?q=1",page:1}).trigger("reloadGrid");
setTimeout(function() { eval(json.mesaj); },timpout+800);
},
error: function (data) {
eroare(true,eroareatn,eroaremsg); return false;
}
}); // ajax
}// function updatedate

e.preventDefault() prevents the kendo 'read' event but does not work for 'create' on update keypress in kendo popup editor

transport: {
parameterMap: function (data, operation) {
if (operation !== "read") {
return JSON.stringify(data);
} else {
return (data);
}
},
read: {
url: function () {
return moduleServiceRoot;
},
type: "GET",
dataType: "json",
async: true
},
create: {
url: function (rec) {
return moduleServiceRoot;
},
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: "json",
async: true
},
complete: function (e) {
$("#grid").data("kendoGrid").dataSource.read();
async: true
},
},
requestStart: function (e) {
console.log('request started');
if (e.type == 'create' & validInput == false) {
console.log('request started');
e.preventDefault();
}
}
in the above code validInput is always false. if i comment out the if statement kendo grid read operation is prevented (grid does not show any data) but if i uncomment it, it won't work for kendo create, when i hit update in kendo popup editor.
create: function (options) {
if (validInput) {
$.ajax({
url: moduleServiceRoot,
dataType: "json",
type: "POST",
contentType: 'application/json; charset=utf-8',
async: true,
data: JSON.stringify(options.data),
success: function (result) { // notify the data source that the request succeeded,
options.success(result);
},
error: function (result) { // notify the data source that the request failed
options.error(result);
}
});
}
}
and it works fine

JSON Stringify Empty string

I've recently ran into a problem with JSON.stringify not taking an empty string.
I solved it with a small fix but i'm still bothered why it doesn't work with an empty string
Here is my code.
$(function () {
$('#searchButton').click(function () {
var test = $("#DownloadsSearch").val();
alert(test);
if (test === "") test = "1";
displayAjaxLoading(true);
$.ajax({
url: '#(Url.RouteUrl("DownloadSearch"))',
contentType: 'application/json',
dataType: 'json',
data: JSON.stringify({
searchText: test,
type: '#Model.Type'
}),
type: 'POST',
success: function (data) {
if (data.length > 0) {
$("#Review").empty();
$.each(data, function (id, options) {
$("<p/>").appendTo("#Review").append($('<a>').attr('href', 'http://www.pvs4you.com/' + options.URL).text(options.ProductID));
// $('<a>').attr('href', options.URL).text(options.ProductID).appendTo("#Review");
});
} else {
$("#Review").empty();
$("<p/>").html("<b>#Model.Title wasn't found.</b>").appendTo($("#Review"));
}
$('#small-searchterms').autocomplete({
disabled: false
});
displayAjaxLoading(false);
},
error: function (xhr, ajaxOptions, thrownError) {
$("#Review").empty();
$("<p/>").html("<b>#Model.Title wasn't found.</b>").appendTo($("#Review"));
}
});
});
});

Resources