2

How to post parameters to [WebMethod] using angularjs.

My angularjs script is::

app.factory('checkAvabService', function ($http) {
    return {
        checkFare: function () {
            return $http.post('onewaydomflt.aspx/checkAvab', { 

             data:{test:"hello"} //post test as parameter to web method

          })
        }
    };
});

My Web Method is::

[System.Web.Services.WebMethod]
public static string checkAvab(string test)  // string test is not accept value "hello"
{
    string x = test;
    return x;
}

Here I am not getting test parameter value hello to WebMethod.

Whats wrong in this code.

1 Answer 1

2

Just pass data in the 2nd parameter of $http.post call which takes data in JSON format. There is no need to use data again in object which you are passing.

return $http.post('onewaydomflt.aspx/checkAvab', {test:"hello"})
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you so much.. Pankaj Parker. Now webmethod accept parameter fine ... thanks again
@JBNizet Thanks man for heads up.. but may I know while posting any data to ASP.Net MVC -4 action, why we need to pass request body in stringified format. Just asking for curiosity..
I don't know anything about ASP.Net MVC -4, and am not sure what you mean by "stringified format".
@JBNizet I meant to say, before sending data to server I stringify it(JSON.strigify(data))..
I don't see why this would be of any use. Angular transforms the objects passed to $http to JSON by default: docs.angularjs.org/api/ng/service/$http#default-transformations If the data property of the request configuration object contains an object, serialize it into JSON format.
|

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.