2016-07-04 6 views
1

Ich habe Probleme, ein JSON-Objekt über eine AJAX POST-Anforderung an die Web-API zu übergeben. Hier ist meine AJAX POST-AnfrageJSON-Daten über AJAX an Web-API senden

<li><a href="#register">Register</a></li> 

$('#usrRegister').click(function() { 
       var uname = $('#usrName').val(); 
       var uloginname = $('#usrLoginName').val(); 
       var uemail = $('#usrEmail').val(); 
       var upwd = $('#usrPwd').val(); 

       var registerObj = { 
        "name": uname, 
        "username": uloginname, 
        "email": uemail, 
        "password": upwd 

       }; 
       console.log("registerObj :", registerObj); 

       $.ajax({      
        url: "http://localhost:54118/api/UserApi", 
        type: "POST", 
        //contentType: "application/x-www-form-urlencoded", 
        data: JSON.stringify(registerObj), 
        contentType: "application/json", 
        success: function (data) { 
         alert("Successfully Registered.."); 
        }, 
        error: function (xhRequest, ErrorText, thrownError) { 
         alert("Failed to process correctly, please try again"); 
        } 
       }); 
      }); 

API:

[HttpPost] 
public void Post(tblUser userdata) 
{ 
    obj.tblUsers.Add(userdata); 
    try 
    { 
    obj.SaveChanges(); 
    } 
    catch (System.Data.Entity.Validation.DbEntityValidationException ex) 
    { 
    foreach (var e in ex.EntityValidationErrors) 
    { 
     //check the ErrorMessage property 
    } 
    } 
} 

Wenn ich registrieren, klicken Sie es bei $.ajax({ in der Konsole und API zeigt nicht anruft. Aber wenn ich contentType durch application/x-www-form-urlencoded anstelle von application/json ersetze, ruft API aber alle Felder als null auf. Wenn ich dieselbe API im REST-Client anrufe, funktioniert es gut. Hilf mir, was das Problem verursacht.

+2

Bitte teilen Sie Ihre Web-API-Controller-Klasse –

+1

Sind Sie sicher, dass Sie api läuft –

+1

, warum Sie die 'JSON.stringify (RegisterObj)' stringfy übergeben Sie stattdessen als: 'data: {'userdata', registerObj}' –

Antwort

2

Danke für die Antwort. Es funktioniert mit diesem Code. Ich habe xhrFields: 'withCredentials:true' und contentType: 'application/x-www-form-urlencoded' hinzugefügt

  $.ajax({      
        url: "http://localhost:54118/api/UserApi", 
        xhrFields: 'withCredentials:true', 
        type: "POST", 
        data: { 
           "name": uname, 
           "username": uloginname, 
           "email": uemail, 
           "password": upwd 
          }, 
        contentType: 'application/x-www-form-urlencoded', 
        success: function (data) { 
         alert("Successfully Registered.."); 
        }, 
        error: function (xhRequest, ErrorText, thrownError) { 
         alert("Failed to process correctly, please try again"); 
        } 
       }); 
+0

Es war das "application/x-www-form-urlencoded" Bit, das es für mich getan hat :) –

2

Versuchen Hinzufügen des [FromBody] Präfix zu Ihrem Parameter.

Gefällt mir: public void Post([FromBody] tblUser userdata).

Dann sollten Sie wieder application/json verwenden können!

+1

@ N123, Ja, es ist ein wichtiger Punkt, den Sie überprüfen müssen –