0

Having problem when using POST using JSON in ASP.NET. Kindly check my code when I used POST. Is there's something wrong when javascript code?

Exact error :

Failed to load resource: the server responded with a status of 405 (Method Not Allowed) localhost:99/Service1.svc/api/updtelogin.json XMLHttpRequest cannot load localhost:99/Service1.svc/api/updtelogin.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:1110' is therefore not allowed access. The response had HTTP status code 405.

IService

    [OperationContract]
    [WebInvoke(Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "api/updtelogin.json")]
    UpdateUser updteUser(RequestData rData);

RequestData

[DataContract(Namespace = "")]
public class RequestData
{
    [DataMember]
    public string details { get; set; }
}

Service.svc

    private UpdateUser updateuser(RequestData rData)
    {
        return updteUser(rData);
    }

    public UpdateUser updteUser(RequestData rData)
    {
        var data = rData.details.Split('|');            

    }

And this is my javascript from ASP.NET

  <script type="text/javascript">
    $("#btnChange").live("click", function () {
        var test = {};
        test.uname = "admin";
        $.ajax({
            type: 'POST',
            url: 'http://localhost:99/Service1.svc/api/updtelogin.json',
            data: "{rData:" + JSON.stringify(test) + "}",
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (r) {
                alert(r.d.uname);
            }
        });
    });
</script>

1 Answer 1

1

Correct me if I'm wrong, you have two applications:

  • localhost:99 - wcf service
  • localhost:1110 - web application calling your wcf service

This violates "same origin policy" for your ajax request because of different port.

Here you have instruction how to add CORS headers to WCF service: Enable CORS on WCF Service. Get HTTP 405: Method Not Allowed

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

2 Comments

What I'm getting now is Bad request POST localhost:99/Service1.svc/api/updtelogin.json 400 (Bad Request)
It looks like deserialization problem: data: "{rData:" + JSON.stringify(test) + "}" does not correspond to your RequestData type: public class RequestData { [DataMember] public string details { get; set; } }

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.