0

I hope you can help, im trying to upgrade a broken login system so it uses POST instead of GET when transffering login credentials.

It never reaches the actual method so the problem is somewhere between the interface and my javascript.

Interface:

[OperationContract]
        [WebInvoke(Method = "POST", 
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json, 
            UriTemplate = "/DoLogin")]
        LoginKey DoLogin(string email, string password,string tenant);

Javascript:

 $.ajax({
            type: "POST",
            url: "RestService.svc/DoLogin",
            data:'{"email":"' + encodeURIComponent(email) + '","password":"' + encodeURIComponent(password) + '","tenant":"' + encodeURIComponent(tenant) + '"}',
            dataType: "json",
            cache: false,
            success: function (loginKey) {
... rest of method

The JSON data i send is valid: { "email": "mail", "password": "somepassword", "tenant": "tenantid" } Any ideas whats going wrong here? It worked just fine with GET

ps here is the first line of the actual login rest service method:

public LoginKey DoLogin(string email, string password, string tenant)
        {
2
  • Can you post the error you're getting? Commented Aug 8, 2011 at 15:44
  • Also, please post your WCF service configuration. Commented Aug 8, 2011 at 16:03

2 Answers 2

2

Try setting the content type also, just in case:

type: "POST",
url: "RestService.svc/DoLogin", data:'{"email":"' + encodeURIComponent(email) + '","password":"' + dataType: "json",
"contentType": "application/json; charset=utf-8", ...

Other than that, your code looks correct although I would prefer a different approach:

public class Credential
{
  public string Email{get;set;}
  public string Password {get;set;}
  public string Tenant {get;set;}
}

And in your WCF Method:

public LoginKey DoLogin(Credential theCredential)

And in your markup:

var theCreadential = new Object();
theCreadential.Email = $('#txtEmail').val();
theCreadential.Password = $('#txtPassword').val();
theCreadential.Tenant = $('#txtTenant').val();
 $.ajax({
            type: "POST",
            url: "RestService.svc/DoLogin",
            data: "{'theCredential': " + JSON.stringify(theCredential) + "}",
            dataType: "json",
            cache: false,
            success: function (loginKey) {
... rest of method
Sign up to request clarification or add additional context in comments.

1 Comment

thx, ContentType did the trick. A better JSON object "prettied" it up though :)
0

I decided to drop the whole restservice and instead migrate the project over to MVC3 which handles these scenarios a lot simpler and easier.

The issue was solved by adding a contentType to the ajax call btw:

 $.ajax({
            type: "POST",
            url: "restservice.svc/dologin",
            data: JSON.stringify(json),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (result) {

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.