Aram Gevorgyan
Aram Gevorgyan

Reputation: 2205

ASP.NET MVC post method parameter is always nothing (null)

I'm sending json to ASP.NET MVC from sencha touch:

Ext.Ajax.request({
    url: 'http://localhost:52771/api/login',
    method: 'post',
    headers: {
             'Content-Type': 'application/json'
               },  
        params: {
            post: JSON.stringify({
                    user: username,
                    pwd: password
                })
        },
    },
    success: function (response) {

        var loginResponse = Ext.JSON.decode(response.responseText);

        if (loginResponse === true) {
            // The server will send a token that can be used throughout the app to confirm that the user is authenticated.
            me.sessionToken = loginResponse.sessionToken;
            me.signInSuccess();     //Just simulating success.
        } else {
            me.signInFailure(loginResponse.message);
        }
    },
    failure: function (response) {
        me.sessionToken = null;
        me.signInFailure('Login failed. Please try again later.');
    }
});

Here is service code:

   Namespace SimpleSevice
Public Class LoginController
    Inherits ApiController

    Private Shared list As IList(Of Login1) = New List(Of Login1)( _
              {New Login1() With {.user = "User", .pwd = "Password"}, _
               New Login1() With {.user = "User1", .pwd = "Password1"}, _
               New Login1() With {.user = "User2", .pwd = "Password2"}
               }
    )

    Public Function GetLogin() As IEnumerable(Of Login1)
        Return list
    End Function

    <System.Web.Http.HttpPost> _
    Public Function PostLogin(ByVal login As Login1) As Net.Http.HttpResponseMessage
        If Login.user = "John" AndAlso Login.pwd = "Human" Then
            Return Request.CreateResponse(Of Boolean)(Net.HttpStatusCode.OK, True)
        End If
        Return Request.CreateResponse(Of Boolean)(Net.HttpStatusCode.NotFound, False)
    End Function

End Class

End Namespace

Here is http traffic:

    Request URL:http://localhost:52771/api/login?_dc=1391752685427
Request Method:POST
Status Code:500 Internal Server Error
Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:53
Content-Type:application/json
Host:localhost:52771
Origin:http://localhost:1841
Referer:http://localhost:1841/login/
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
X-Requested-With:XMLHttpRequest
Query String Parametersview sourceview URL encoded
_dc:1391752685427
Request Payload
post=%7B%22user%22%3A%22t%22%2C%22pwd%22%3A%22y%22%7D
Response Headers
Access-Control-Allow-Origin:http://localhost:1841
Cache-Control:no-cache
Content-Length:900
Content-Type:application/json; charset=utf-8
Date:Fri, 07 Feb 2014 05:58:18 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?RDpcc2VuY2hhXFNpbXBsZVNldmljZVxTaW1wbGVTZXZpY2VcYXBpXGxvZ2lu?=

I'm getting login always Nothing.

Upvotes: 0

Views: 444

Answers (2)

If you want to use default headers, use it with Ext.Ajax like this, before you work with the request.

Ext.Ajax.defaultHeaders = {
   'Content-Type' : 'application/json'
};

If you want to specify the header per-request, use this.

Ext.Ajax.request({
    url: 'http://localhost:52771/api/login',
    method: 'post',
    headers: { 'Content-Type' : 'application/json' }, // rest of the code

Basically, you need to ensure Content-Type request header is sent in the request with a value of application/json.

However, as pointed out by Darrel, you are using the attributes from the ASP.NET MVC framework. It still works correctly in your case simply because you have named the action methods correctly as per the convention and attributes are not coming into the picture at all. Use System.Web.Http.HttpPost. Better yet, just remove them.

UPDATE:

Instead of

params: {
    post: JSON.stringify({
        user: username,
        pwd: password
    })
},

use

jsonData: { user: 'john', pwd: 'pass' }

Upvotes: 1

Darrel Miller
Darrel Miller

Reputation: 142054

ASP.NET MVC and ASP.NET Web API are two completely different frameworks. They can sit side by side in the same project but they use a completely different set of classes. The attributes you are using on your actions in you ApiController are from the MVC namespace, hence they are not valid.

Upvotes: 1

Related Questions