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"));
}
});
});
});
Related
I works with ASP.net MVC and I have get "Error" from belowe my JavaScript code:
This is my AJAX jQuery code:
$(document).ready(function () {
var dataId;
$(".item2 .small-img").click(function () {
dataId = $(this).data("id");
var url = '#Url.Action("DecorationTest", "Carpet")' + "?Id=" + dataId + "&title=" + '#Url.ToFriendlyUrl(Model.Title.ToString())';
$('#dateLink').prop('href', url);
$.ajax({
url: '/Carpet/CheckCarpet',
type: 'POST',
dataType: 'Json',
data: '{"CarpetImageid":"' + dataId + '"}',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data.Success == false)
$('#dateLink').hide();
else
$('#dateLink').show();
alert("Success");
},
error: function (errorThrown) {
alert("Error");
}
});
and this is a controller code:
[HttpPost]
public ActionResult CheckCarpet(int CarpetImageid)
{
bool DecorShow = false;
DecorShow = db.Images.Where(x => x.Id == CarpetImageid).FirstOrDefault().DecorTest;
return Json(new JsonData { Success= DecorShow });
}
what's the reason? Someone knows?
I found the solution.i change url to #Url.Action("CheckCarpet", "Carpet") and fixed.
$(".small-img").click(function () {
dataId = $(this).data("id");
var url = '#Url.Action("DecorationTest", "Carpet")' + "?Id=" + dataId + "&title=" + '#Url.ToFriendlyUrl(Model.Title.ToString())';
$('#dateLink').attr('href', url);
$.ajax({
url:'#Url.Action("CheckCarpet", "Carpet")',
type: 'Post',
dataType: 'Json',
data: '{"CarpetImageid":"' + dataId + '"}',
contentType: 'application/json; charset=utf-8',
success: function (result) {
if (result.Success == false) {
$('#dateLink').hide();
$('.p2').hide();
}
else {
$('#dateLink').show();
$('.p2').show();
}
},
error: function (error) {
alert(error.d);
}
});
});
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
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");
}
});
}
I have created ajax method in one class in my js file. Below is enclosed for the reference
var ajaxcall =
{
SitePath: '',
data: '',
url: '',
callbackfunction: '',
fileElementId: '',
AjaxRequest: false,
callback: true,
async: false,
folder: '',
filename: '',
Call: function () {
if (ajaxcall.AjaxRequest == true) {
alert(ajaxcall.AjaxRequest);
return;
}
else {
try {
ajaxcall.AjaxRequest == true;
alert('b');
$.ajax({
type: "POST",
url: ajaxcall.url,
data: ajaxcall.data,
contentType: "application/json; Characterset=utf-8",
dataType: "json",
async: false,
success: function (data) {
if (ajaxcall.callback == true) {
ajaxcall.callbackfunction(data);
}
},
error: function (request, status, error) {
//alert("Exception Handling : \n" + request.responseText);
alert('Unable to process the request at this moment! Please try again later.');
},
complete: function () {
ajaxcall.AjaxRequest = false;
}
});
}
catch (e) {
ajaxcall.AjaxRequest == false;
// alert("Error Catch : " + e.Description + '\n' + 'Message: ' + e.Message);
}
}
},
AjaxFileUpload: function () {
$.ajaxFileUpload({
type: "POST",
url: "../GenericHandlers/FileUploader.ashx?path=" + ajaxcall.folder,
dataType: 'json',
async: false,
secureuri: false,
fileElementClass: ajaxcall.fileElementClass,
success: function (data) {
var data = data.toString();
ajaxcall.filename = data.substring(6, data.length - 7);
alert(ajaxcall.filename);
return true;
}
});
}
};
Now i want to show a div when ajax call starts and hide after finish.
So for that i have used
$(document).ready(function(
$('#Loading').ajaxStart(function () {
alert('a');
$('#Loading').show();
}).ajaxStop(function () {
$('#Loading').hide();
});
});
But when i call the ajax method (defined above in the class), control goes into ajax method first then in ajaxStart.
I don't know why it is happening. Please help.
Use the recommended global for these:
$.ajaxStart(function() {
$("#Loading").show();
});
$.ajaxComplete(function() {
$("#Loading").hide();
});
Try it this way attached to your Loading id element:
$("#Loading").ajaxStart(function() {
$(this).show();
});
$("#Loading").ajaxComplete(function() {
$(this).hide();
});
AjaxStart called when the http request start, not when the ajax method executes.
This is my jquery with json
$('#btnVerificationOk').click(function () {
var verId = $('#trans_verification_id').val();
var verCode = $('#trans_verification_code').val();
$.ajax({
url: '/Profile/CompleteTransactions',
type: 'POST',
data: { },
dataType: 'json',
success: function (result) {
alert(result);
},
error: function () {
alert('ERROR ERROR !!!!!!!!!!!!');
}
});
});
And My C# method:
[Authorize]
[HttpPost]
private JsonResult CompleteTransactions()
{
return Json("Done");
}
Its always alerts 'ERROR ERROR !!!!!!!!!!!!' i tried debugging but CompleteTransactions method is not firing
And this is my second json which is bellow and works good
$('#btnTransfareOk').click(function () {
var userName = $('#transfare_username').val();
var amount = $('#transfare_amount').val();
if (userName == '' || amount == '') {
$('#transfare_error_list').html('Please fill boxes.');
} else {
$.ajax({
url: '/Profile/TranfareMoney',
type: 'POST',
data: { ToUsername: userName, Amount: amount },
dataType: 'json',
success: function (result) {
$('#transfare_error_list').html(result.text);
$('#trans_verification_id').val(result.id);
$('#transfare_username').val("");
$('#transfare_amount').val("");
},
error: function () {
$('#transfare_error_list').html('Oops... Error.');
}
});
}
});
I'm not 100% sure, but shouldn't you controller action handler be public ?