Json error 400

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 Jahre weitere
Hello Forum

I have created a test API in Admin/Customercontroller.

MVC
public JsonResult Test(int id)
        {

            return Json(new
            {
                Id=id,
                Data = "OK",

            });
        }

When I call it from  $.ajax it works fine

I created a similar call with [httpPost]

ViewModel

public class Testdata {
        public int Id { get ; set ; }
    }

Controller:

  [HttpPost]
        public JsonResult Test(Testdata test)
        {

            return Json(new
            {
                Id = test,
                Data = "OK",

            });
        }

and I call it from Jquery

var url = " http://localhost:15536/Admin/Customer/Test";
var data = { Id: 7  };

$.ajax({
                type: "POST",
                url: url,
                data: data,
                contentType: "application/json; charset=utf-8", // this
                dataType: "json", // and this
                success: function (msg) {

                    console.info("--->", msg);
                },
                error: function (errormessage) {
                    console.info("--->", errormessage);


                    //do something else
                }
            });

but I get error 400 badrequest:
POST http://localhost:15536/Admin/Customer/Test 400 (Bad Request)

Any Ideas ?
4 Jahre weitere
basbos wrote:
Hello Forum

I have created a test API in Admin/Customercontroller.

MVC
public JsonResult Test(int id)
        {

            return Json(new
            {
                Id=id,
                Data = "OK",

            });
        }

When I call it from  $.ajax it works fine

I created a similar call with [httpPost]

ViewModel

public class Testdata {
        public int Id { get ; set ; }
    }

Controller:

  [HttpPost]
        public JsonResult Test(Testdata test)
        {

            return Json(new
            {
                Id = test,
                Data = "OK",

            });
        }

and I call it from Jquery

var url = " http://localhost:15536/Admin/Customer/Test";
var data = { Id: 7  };
data=addAntiForgeryToken(data);

$.ajax({
                type: "POST",
                url: url,
                data: data,
                contentType: "application/json; charset=utf-8", // this
                dataType: "json", // and this
                success: function (msg) {

                    console.info("--->", msg);
                },
                error: function (errormessage) {
                    console.info("--->", errormessage);


                    //do something else
                }
            });

but I get error 400 badrequest:
POST http://localhost:15536/Admin/Customer/Test 400 (Bad Request)

Any Ideas ?
4 Jahre weitere
Hi,

Try to add [IgnoreAntiforgeryToken] parameter to the action and if this fixes the problem when you are making POST requests there is an issue with the anti-forgery token.

Regards,
Stoyan
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.