JSON形式のPOSTを使用して名前と電話番号をasp.netのwebApiMVC4に送信し、データベースにSQLサーバーを使用する単純なAndroidアプリを作成しています。問題は、私がそれを正しく理解できないようであり、webapiに情報を送信するときにデータベースに何も投稿しないことです。
fiddlerを使用してJSONを送信すると機能しましたが、問題が何であるかわかりません。助けてください、私はもうしばらく立ち往生しています。
これが私のJqueryajaxクライアントコードです:
$.ajax({
type: "POST",
data: {"Name" : "JUJI", "PhoneNumber" : 999},
url: "http://127.0.0.1:52380/api/Default1",
dataType: "json",
success:function(response){
alert("yupie");
console.log(response);
},
error:function(response){
alert("no response");
console.log(response);
}
});
webApiのサーバー側のモデルコードは次のとおりです。
public class Contact
{
public int ID { get; set; }
public string Name { get; set; }
public double PhoneNumber { get; set; }
}
サーバー側のコントローラー:
// POST api/Default1
public HttpResponseMessage PostContact(Contact contact)
{
if (ModelState.IsValid)
{
db.Contacts.Add(contact);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, contact);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = contact.ID }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}