Ajax call showing error when call ActionResult in MVC.NET

Amandeep Singh

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.

Amandeep Singh

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.

edited at
0

Comments

0 comments
Login to comment

Related

Asp.Net - How to call ActionResult with ajax

Ajax call with .net MVC

jquery $.ajax call for MVC actionresult which returns JSON triggers .error block

Redirect to error page from ajax call in ASP.net MVC

Validation error from model on ajax call asp.net MVC

jQuery Ajax Call returns error : ASP.NET MVC

Call ActionResult when a checkbox is checked

MVC - Is it possible to call an actionResult function from another ActionResult function when return views are different

Ruby on rails Ajax call showing 404 error

ajax call with jquery asp.net mvc

C# & ASP.NET MVC : call a view with ajax call

Call ajax when MVC project starts

How to dynamically call an ActionResult in your API by using an Ajax-call?

jQuery ajax call get API that returns Task<ActionResult<List<ApiObject>>> keeps getting 500 error response

jquery promise return when ajax call error

Ajax call doesn't work when debug="false" (ASP.NET MVC)

Jquery ajax call fails when connecting to remote database in asp.net mvc 5

MVC Ajax Call issue

MVC and fail AJAX call

Ajax call to MVC controllers

Ajax call is not working, "404 not found error" showing in Yii Framework

Ajax call type POST showing undefined index error

Highcharts Data Not Showing on Ajax Call

Ajax unable to call function, ASP.net MVC

Asp.net MVC Ajax call not calling controller method

ASP .NET MVC - return Exception message to ajax call

How to refresh partialview with Ajax call in ASP.NET MVC?

AJAX call in ASP.NET MVC application not calling Action method

Render a View after an AJAX call in asp.net MVC