3

For passing an email address I'm using ajax with POST as type.

$.ajax({
    url: "api/Search/UserByEmail",
    type: "POST",
    data: JSON.stringify({ emailAddress: userEmail }),
    contentType: "application/json;charset=utf-8",
    dataType: "json",
    success: function (data) { ... }
});

Controller:

[HttpPost]
public IEnumerable<Object> UserByEmail([FromBody] string emailAddress) { ... }

That's what Fiddler says:

POST http://localhost:52498/api/Search/UserByEmail HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json;charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:52498/#
Accept-Language: de-DE
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Host: localhost:52498
Content-Length: 35
DNT: 1
Connection: Keep-Alive
Pragma: no-cache

{"emailAddress":"[email protected]"}

Why is the emailAddress parameter always null?

3
  • This looks fine to me, have you tested constructing a request with Fiddler and posting manually? Most likely problem is with the JSON data but I can't see anything wrong with it from here. You could also try removing the [FromBody] attribute. Commented Oct 2, 2013 at 8:09
  • When I take out [FromBody], my method is not even invoked. Here's the Routing: config.Routes.MapHttpRoute( name: "UserByEmailRoute", routeTemplate: "api/Search/UserByEmail", defaults: new { controller = "Search", action = "UserByEmail" } ); Commented Oct 2, 2013 at 10:37
  • 1
    that probably makes sense, it would be expecting you to post to api/Search/UserByEmail/emailAddress without that - just trying to rule out things that may be causing the problem :) In Fiddler, if you post [email protected] as the body does it work? Commented Oct 2, 2013 at 10:39

3 Answers 3

1
 // JS - jQuery 
 $.ajax({
        url: "/Home/UserByEmail",
        type: "POST",
        data: { emailAddress: "[email protected]" },
        dataType: "json",
        success: function (data) { if(data != null) { alert(data.toString()); } }
    });



  [Serializable]
  public class EmailFormModel {
     public string emailAddress { get; set; }
  }

    [HttpPost]
    public JsonResult UserByEmail(EmailFormModel emailFormModel)
    {
        bool ok = emailFormModel.emailAddress != null;
        return Json(new { ok }); 
    }

Use a formModel and put a serializable attribute on the class and it will serialize your javascript automatically to a C# equivalent. And you don't need to use Json-stringify.

Note a removed the // contentType: "application/json;charset=utf-8", declaration from the ajax-method. I've actually never used it.

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

9 Comments

"You don't need to use JSON.stringify" - you do if you want to pass data as JSON...
No if you have a serializable class as a parameter into your method it will automatically convert the javascript object to C#. And I have used this a lot myself, and it's nice!!
Yes, the point I am making though you are no longer working with JSON - the data is converted to a query string and appended to the URL.
Yeah but it's still a HTTP POST!
yep it is, but like I said you are no longer posting JSON from the client so application/json would have to also change to application/x-www-form-urlencoded.
|
0

I think the JSON.stringify could be your problem. MVC will handle the serialization/deserialization of parameters for you, change it to:

data: { emailAddress: userEmail }

2 Comments

@user2558051 Also, take out that FromBody attribute, I'm pretty sure you don't need that (I've never ever heard of it!)
@mattytommo you generally do need the [FromBody] attribute with the WebAPI...however, I get the feeling that might be the problem here as well (see my comment).
0

Modify the data property in the original ajax call to

data: '"' + userEmail + '"',

Getting some of these Web API calls to work can sometimes be a little tricky

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.