0

I have gone through this solution on stackoverflow but I couldn't solve my problem.

In HomeController I have a method named as Audit which I want to be posted from /Home/Index page's script through jQuery. The controller looks like:

public class HomeController : Controller
{
  [HttpPost]
  [ValidateAntiForgeryToken]

  public JsonResult Audit([FromBody] JObject jObject)
  {
       if (jObject != null)
       {
           return Json("success");
       }
       return Json("failed");
  }
}

In the /Home/Index pages's javascript file I have tried to post a JSON Object to that Audit in a way like this:

var auditData = {};
$(document).ready(function(){
    var request = $.getJSON('http://www.geoplugin.net/json.gp', function (responseData, status) {
        auditData = {
            Latitude : responseData.geoplugin_latitude,
            Longitude : responseData.geoplugin_longitude
        };

        $.post('Audit', auditData, function (response) {
            console.log(response);
        });
    });
});

I want the auditData object to be posted as JObject in /Home/Audit but something is going wrong. I think there is problem either in controller or, in $.post method. How can I solve this problem?

3
  • Have you debugged the request to see what the error is? I can see you're not passing an anti-forgery token, so that attribute will be causing issues if nothing else Commented Sep 7, 2019 at 15:12
  • @RoryMcCrossan Debugging says: Failed to load resource: the server responded with a status of 404 (Not Found) Commented Sep 7, 2019 at 15:43
  • There's your first issue then. The 'Audit' path isn't valid. I'd suggest trying '/Home/Audit' Commented Sep 7, 2019 at 15:45

2 Answers 2

1

Your post URL is wrong, and you need to name the data you're posting back as jObject as well to match what you defined in your controller.

$.post('@Url.Action("audit", "home", new { area = "" })', { jObject: auditData }, 
  function (response) {
    console.log(response);
});

Sign up to request clarification or add additional context in comments.

Comments

0

There are multiple issues in your current code, check points below one by one:

  1. As the suggestion from Rory, your request url is wrong, which should be Home/Audit
  2. If you post request without antitoken, you should remove [ValidateAntiForgeryToken]
  3. You should post request data with json instead of form data.

Code:

Client

@section Scripts{
    <script type="text/javascript">
        var auditData = {};
        $(document).ready(function(){
                auditData = {
                    Latitude : "l1",
                    Longitude : "l2"
                };
            $.ajax({
                type: 'POST',
                url: 'Home/Audit',
                data: JSON.stringify(auditData),
                success: function(data) { alert('data: ' + data); },
                contentType: "application/json"
            });
        });
    </script>
}

Server:

public class HomeController : Controller
{
    [HttpPost]
    //[ValidateAntiForgeryToken]

    public JsonResult Audit([FromBody]JObject jObject)
    {
        if (jObject != null)
        {
            return Json("success");
        }
        return Json("failed");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.