0

I have this method to send some data with ajax:

function SendData(foodID, basketID) {
    var data = { FoodID: foodID, BasketID: basketID };

    $.ajax({
        url: '/Order/Post',
        type: 'POST',
        data: JSON.stringify(data),
        contentType: 'application/json;utf-8',
        datatype: 'json'
    }).done(function (data) {
        console.log(data);
    }).fail(function (data) {
        console.log("Error: " + data);
    });
}

In C#, my Post Method in my Order controller gets triggered, but the string I want to hand over is null:

public bool Post(string s)
{
    //When this gets executed, s is null
    return true;
}

I tested this by executing SendData(1,1) directly on a button click. What is the mistake I'm doing and how can I get the string in my Post-Method?

2
  • have you tried without stringify? data: data, Commented Apr 22, 2021 at 8:19
  • That doesn't help either Commented Apr 22, 2021 at 8:22

3 Answers 3

1

you are post the object. not string.

you can try generate to object and load this object. or add new one parameter to action (foodId and basketId but you must post like that if you check this option data:{foodId,basketId})

//model
    public class SomeObject{
public string FoodId {get;set;}
public string BasketId {get;set;}
}

//code
public bool Post(SomeObject data)
{
    return true;
}
Sign up to request clarification or add additional context in comments.

Comments

0

It seems for me your data structure to the Post action doesn't match action parameter names. Try this:

[HttpPost]
public bool Post(string foodID, string baskedID)
{
    return true;
}

Comments

0

I believe you have to name your data that's being passed like so:

 data: { 's': JSON.stringify(data) }

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.