1

I am making an web api in Asp.Net MVC5 with web api2. I made a post Like this:

public class SmsClientsController : ApiController
{
    public void Post(Client client)
    {
        //Add Client to database
    }
}

the Model Client is like this:

public class Client
{
    public int Id { get; set; }
    [Required]
    public string Username { get; set; }
    [Required]
    public string Password { get; set; }
    public int Role { get; set; }
}

and I called this post method on a button click, simply from javascript in client side like this:

function SendSms() {
    var studentData = {
        "Username": "Anjin",
        "Password": "Pradhan",
        "Role": "1"
    };
    $.ajax({
        type: "POST",
        url: "http://192.168.0.102/ProductsApp/api/SmsClients",
        data: JSON.stringify(studentData),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        processData: true,
        success: function (data, status, jqXHR) {
            console.log(data);
            console.log(status);
            console.log(jqXHR);
            alert("success..." + data);
        },
        error: function (xhr) {
            alert(xhr.responseText);
        }
    });
}

But on response it just alerts a blank alert box. And when i checked the console through fire bug there was an error:

"NetworkError: 405 Method Not Allowed - http://192.168.0.102/ProductsApp/api/SmsClients"

Why is so happening?? Is this Post method invalid. Please help me to correct this...

3 Answers 3

3

WITHIN THE SAME PROJECTS (SAME DOMAIN)

url: "http://192.168.0.102/ProductsApp/api/SmsClients",

If both WebAPI and MVC Pages are in the same project, I mean in same domain, then url should be as shown below -

url: "/api/values",

IN DIFFERENT PROJECTS (DOMAINS)

If they are in different projects, then you need to enable cors. To enable cors, first get the following nuget.

PM> Install-Package Microsoft.AspNet.WebApi.Cors

Then enable cors in WebApiConfig.cs -

config.EnableCors();

Then in the controller action you can place this attribute. Make sure you allow only proper origins instead of *

[EnableCors("*", "*", "*")]

And now from other project, you can make a request in the following way -

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>

    function SendSms() {
        var studentData = {
            "Username": "Anjin",
            "Password": "Pradhan",
            "Role": "1"
        };
        $.ajax({
            type: "POST",
            url: "http://localhost:23133/api/values",
            data: JSON.stringify(studentData),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processData: true,
            success: function (data, status, jqXHR) {
                console.log(data);
                console.log(status);
                console.log(jqXHR);
                alert("success..." + data);
            },
            error: function (xhr) {
                alert(xhr.responseText);
            }
        });
    }
</script>
<input type="submit" onclick="SendSms()" value="Click" />
Sign up to request clarification or add additional context in comments.

Comments

0

I don't know your configuration, but by default a web page cannot make calls to services (APIs) on a domain other than the one where the page came from. You most likely just have to implement CORS support.

http://blogs.msdn.com/b/carlosfigueira/archive/2012/02/20/implementing-cors-support-in-asp-net-web-apis.aspx

Comments

0

Just Changing the ip address (192.168.0.102) with localhost worked for me in my local server

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.