0

I have POST method like this

    public void PostMethod([FromBody]string param)
    {
        var test = param;
    }

And simple JS script

<script type="text/javascript">
$.ajax({
    url: 'http://localhost:8101/sd/Localization/PostMethod',
    type: 'POST',
    data: {
        param: 'test'
    }
});
</script>

Ot work great. Ajax call is invoking the method BUT the param is null :/ always. I don`t know why. I can't get it running. Can someone pls help me and show me the correct direction ?

// update

too check something i`ve created new project to be sure that i didn't break something. Even in new project in default config I always get null

1 Answer 1

2

Because param is a simple type, so to make it work for simple type, you just:

<script type="text/javascript"> 
$.ajax({ 
    url: 'http://localhost:8101/sd/Localization/PostMethod', 
    type: 'POST', 
    data:  {"" : 'test' } 
    } 
}); 
</script>

If you still want to make POST request like yours, define you own strong type model:

class YourModel
{
    public string Param {get; set;}
}

So your action:

public void PostMethod(YourModel param)   
{   
    var test = param;   
}

Or use JObject:

public void PostMethod(JObject param)   
{   
    var test = param;   
}

More information:

http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1

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

3 Comments

Thank you for help. I'm testing in on simple type. What if I want to use a more complex type. What do I need to change ? The second thing I'll be trying to achive is sending this from C# not with JS
ok last question. Do you know some good samples of sending same post request but with c# ?
@Fixus: You should ask another question instead of using comment, anyway you can use HttpClient for this, lot of questions on stackoverflow. For sample: blogs.msdn.com/b/webdev/archive/2012/08/26/…

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.