I am using Angularjs with asp.net MVC. I want to save data using ajax hit but it is showing Error message And in browser it is showing "Failed to load resource: the server responded with a status of 500 (Internal Server Error)". I want to call Action result once for test with a static id but it is not working. My View page is:
@{
ViewBag.Title = "SaveOwn";
}
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
(function (angular) {
'use strict';
angular.module('SaveOwnData', [])
.controller('SaveOwnDataController', [
'$scope', function ($scope) {
$scope.saveData = {
id: '',
name: '',
designation: '',
mobile: ''
}
$scope.save = function () {
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: { "id": "1" },
url: "/Home/SaveData",
success: function () {
alert("Success");
},
error: function () {
alert("error");
}
});
};
}]);
})(window.angular);
</script>
<div ng-app="SaveOwnData" ng-controller="SaveOwnDataController as saveData" ng-init="id=0; name=0;designation=0; mobile=0 ">
<input type="text" ng-model="saveData.id" />
<input type="text" ng-model="saveData.name" />
<input type="text" ng-model="saveData.designation" />
<input type="text" ng-model="saveData.mobile" />
<input type="button" data-ng-click="save()" value="Save" />
{{saveData.total()}}
{{saveData.mobile}}
</div>
ActionResult code is:
public ActionResult SaveData(string id)
{
return View();
}
i want to call SaveData method once as demo with ajax then i will use all parameters. Please help.
Thanks in advance.
Thanks to all for help me but there was another problem. The problem is resolved after using JSON.stringify in my code i have successfully called to action as :
$scope.save = function () {
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ "id": "1" }),
url: "/Home/SaveData",
success: function (data) {
alert(data.status);
},
error: function () {
alert("error");
}
});
};
}]);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments