I m getting Internal server error (500) making an AJAX request. I m not sure why the request is failing. Need help in this. Here is my code.
$('#LanguageId').on('change', function(){
var setValue =JSON.stringify( $('#LanguageId').val());
console.log(typeof(setValue));
$.ajax({
url: '/RootObjects/SaveLanguage',
type: 'POST',
dataType: 'json',
data:setValue ,
contentType: 'application/json; charset=utf-8',
success: function (data) {
}
});
})
and the controller's action
[HttpPost]
public ActionResult SaveLanguage(string LanguageId)
{
GlobalVariables.LangId = LanguageId.ToString();
return View("Index");
}
can't understand what is wrong
Change your ajax request like this:-
$('#LanguageId').on('change', function(){
var setValue =$('#LanguageId').val();
console.log(typeof(setValue));
$.ajax({
url: '/RootObjects/SaveLanguage',
type: 'POST',
dataType: 'json',
data:{'LanguageId':setValue} ,
success: function (data) {
}
});
})
Related
im new in JS,im looking for a way to create a class or function,reusable everywhere in my code,just pass it parameters and get the result,because currently I am doing like this:
$.ajax({
dataType: "json",
type: "POST",
url: "#Url.Action("power","Ranking")",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ "regionalManager": tmpString }),
success: function (result) {
})}
I write this every time I need, and im sick of it,
function sendAjaxCustom(DataType,Type,Url,Ctype,Data){
$.ajax({
dataType: DataType,
type: Type,
url: Url,
contentType: Ctype,
data: Data,
success: function (result) {
return result;
})}
}
You can call this function in JS like
var result = sendAjaxCustom("json","POST",'#Url.Action("power","Ranking")',"application/json; charset=utf-8",JSON.stringify({ "regionalManager": tmpString }));
you will have the result in result variable.
You can create a function like this
function ajax(url, data) {
$.ajax({
dataType: "json",
type: "POST",
url: url,
contentType: "application/json; charset=utf-8",
data: data,
success: function (result) {
})}
}
Pass the url if it's dynamic and the object data on the second parameter.
Just create a simple function with your variables that need to change between calls and return the $.ajax result from there.
function ajaxWrapper(url, data, callback) {
return $.ajax({
dataType: 'json',
type: 'POST',
url: url,
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data),
success: callback
});
}
When you want to call it:
ajaxWrapper('http://www.google.com/', { hello: 'world' }, function(result) {
console.log(result);
});
With the callback it's much more reusable, since you can use this anywhere and change what you do on completion of the function wherever you use it.
A simple solution is to return an object and pass it to the ajax and if some change is required then you can update the properties of the object before calling the ajax service
function commonAjaxParams() {
return {
dataType: "json",
type: "POST",
url: "#Url.Action("power","Ranking")",
contentType: "application/json; charset=utf-8",
//and so on that are common properties
}
}
//now in your application first call the function to get the common props
var commonParams = commonAjaxParams();
//change or add an parameter to your liking
commonParams.type = 'GET';
commonParams.success = function(){...} //if this action is need
commonPramss.error = function(){...}
//now call you ajax action
$.ajax(commonParams)
There is another way in which you may call the ajax function and you may get success, fail response return.
The benefit is you manage success or fail response independently for each ajax request.
$(document).ready(function() {
function ajaxRequest(dataType, requestMethod, dataURL, jsonData) {
return $.ajax({
dataType: dataType,
type: requestMethod,
url: dataURL,
contentType: "application/json; charset=utf-8",
data: JSON.stringify(jsonData)
});
}
var jsonData = {
"regionalManager": "jason bourne"
};
ajaxRequest(
"json",
"POST"
"#Url.Action('power','Ranking')",
jsonData)
.success((data) {
console.log("success");
}).error((err) {
console.log("error");
}).done(() {
console.log("done");
});
});
I have looked at many posts on SO to figure out my situation but with not much success. I'm trying to send data from a javascript function(DropdownChange()) to MVC controllers Action method as a parameter based on the dropdown value that is selected on the view. Here is my code:
In .cshtml
//code for dropdown
....followed by
//code for creating a new plan
<a href="#Url.Action("ActionMethod")">
<i class="sth" id="sth" Title="Create"></i>
</a>
In .JS file, function to get dropdown change value
function DropdownChange(){
var Value = parseInt($("#dropDownId").val());
$.ajax({
type: "POST",
url: '# Url.Action("ActionMethod", "Home")',
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: {data : JSON.Stringify(Value)},
success: function (data) {
alert('success');
},
error: error: function (result){
alert('error');
}
});
and Here is my Controller action method
[HttpPost]
public ActionResult ActionMethod(string Value)
{
// do something with Value
}
All I'm getting is an error alert message and a JS runtime error along with a null value for Value parameter. Can any one help me in this scenario.
Thanks in advance.
Try
$.ajax({
type: "POST",
url: '# Url.Action("ActionMethod", "Home")',
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: JSON.stringify({data: Value}),
success: function (data) {
alert('success');
},
error: error: function (result){
alert('error');
}
});
OR
$.ajax({
type: "POST",
url: '# Url.Action("ActionMethod", "Home")',
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: Value,
success: function (data) {
alert('success');
},
error: error: function (result){
alert('error');
}
});
I am working on a application that uses a REST architecture style. Hence, I often do some calls to the backend with JSON data.
jQuery is new to me and up to now, the only syntax that worked for me is :
$.ajax({
url: '/api/something/',
type: 'POST',
contentType:"application/json", //Or I will get 400 : Bad request
data: JSON.stringify({key : "value"}),
success: function (data) {
console.log("data : %o", data);
}
});
But I do not want to write explicitly contentType:"application/json" and JSON.stringify at every call. I would prefer something like :
$.someFunction({
url: '/api/something/',
type: 'POST',
data: {key : "value"},
success: function (data) {
console.log("data : %o", data);
}
});
Maybe I could make a function in order to factorize this but I feel that jQuery should have a pre-existing function for that.
Anyone knows such function?
Just wrap everything in a function and pass the parameters you need.
var thinPostWrapper = function(url, data, success) {
return $.ajax({
url: url
type: 'POST',
contentType: "application/json",
data: JSON.stringify(data),
success: success
});
}
thinPostWrapper('/api/something/', {"key" : "value"}, function(data) {
console.log("data : %o", data);
});
Try extending jQuery:
$.extend({doPost: function(url, data, success, error) {
$.ajax({
url: url,
type: 'POST',
contentType: "application/json",
data: JSON.stringify(data),
success: success,
error: error
});
}
});
$.doPost('/api/something/', {"key" : "value"}, function(data) {
console.log("data : %o", data);
});
Try
$.post(
'api/something',
{ key: 'value' },
function( data ) {
...
},
'json'
);
See http://api.jquery.com/jquery.post/ for more details
Try with $.post
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
This:
$.ajax({
url: '/Merchant/SaveDirty',
type: 'POST',
dataType: 'json',
data: ko.toJSON(dirtyItems),
contentType: 'application/json; charset=utf-8',
success: function (data) {
// get the result and do some magic with it
//var message = data.Message;
alert(ko.toJSON(dirtyItems));
}
});
...currently calls this:
[HttpPost]
public void SaveDirty(string json)
{
}
...but when I hit the breakpoint in SaveDirty, no data is passed. I've verified that ko.toJSON(dirtyItems) returns a JSON string in the javascript. What am I doing wrong?
Thanks!
#KillingsWorth, is there any specific reason for which you are posting a JSON string? If not then, you could create a class corresponding to dirtyitems type and in your controller method you can accept a list of dirtyItems.
Class DirtyItem
{ // dirty item properties }
[HttpPost]
public void SaveDirty(List<DirtyItem> dirtyItems)
{
}
you can use the following:
$.ajax({
url: '/Merchant/SaveDirty',
type: 'POST',
dataType: 'json',
data: JSON.stringify(dirtyItems),
contentType: 'application/json; charset=utf-8',
success: function (data) {
///
///
}
});
But if you are using knockout.js in your applicantion then you should do the following:
$.ajax({
url: '/Merchant/SaveDirty',
type: 'POST',
dataType: 'json',
data:JSON.stringify(ko.mapping.toJS(dirtyItems)),
contentType: 'application/json; charset=utf-8',
success: function (data) {
// get the result and do some magic with it
//var message = data.Message;
alert(ko.toJSON(dirtyItems));
}
});
This should works:
$.ajax({
url: '#Url.Action("SaveDirty", "Merchant")'
type: 'POST',
dataType: 'json',
data: JSON.stringify(dirtyItems),
contentType: 'application/json; charset=utf-8',
success: function (data) {
// get the result and do some magic with it
//var message = data.Message;
alert(ko.toJSON(dirtyItems));
}
});
Here is the code I am using to access my web API controller named Owner; the success function is not being called. Any ideas?
$.ajax({
type: "GET",
url: 'http://localhost:26533/api/Owner',
contentType: "application/json",
dataType: "jsonp",
success: function (response) { alert("yes"); }
});
Remove the contentType and dataType and check the response..
Here an example:
$.ajax({
type: 'GET',
url: 'http://localhost:26533/api/Owner',
success: function(data){
alert(data);
},
error: function(xhr, type, exception) {
// if ajax fails display error alert
alert("ajax error response type " + type);
}
});
With this you can see what's wrong...