gpt4 book ai didi

javascript - 我的 ajax 调用应该是什么样子 - jQuery、MVC4?

转载 作者:行者123 更新时间:2023-12-01 03:38:33 25 4
gpt4 key购买 nike

这是我使用 Visual Studio 2010 中的向导创建的 Api Controller :

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using ApiTest2MvcApplication.Models;
using System.Linq.Expressions;
using ApiTest2MvcApplication.DTOs;
using System.Threading.Tasks;
using System.Web.Http.Description;
using System.Web.Mvc;

namespace ApiTest2MvcApplication.Controllers
{
public class Default1Controller : ApiController
{
private UsersContext db = new UsersContext();

// GET api/Default1
public IEnumerable<UserProfile> GetUserProfiles()
{
return db.UserProfiles.AsEnumerable();
}

// GET api/Default1/5
public UserProfile GetUserProfile(int id)
{
UserProfile userprofile = db.UserProfiles.Find(id);
if (userprofile == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}

return userprofile;
}


// PUT api/Default1/5
public HttpResponseMessage PutUserProfile(int id, UserProfile userprofile)
{
if (ModelState.IsValid && id == userprofile.UserId)
{
db.Entry(userprofile).State = EntityState.Modified;

try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}

return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}

// POST api/Default1
[AcceptVerbs("POST")]
public HttpResponseMessage PostUserProfile(UserProfile userprofile)
{
System.Diagnostics.Debug.WriteLine("Username: " + userprofile.UserName);
if (ModelState.IsValid)
{
db.UserProfiles.Add(userprofile);
db.SaveChanges();

HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, userprofile);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = userprofile.UserId }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}

// DELETE api/Default1/5
public HttpResponseMessage DeleteUserProfile(int id)
{
UserProfile userprofile = db.UserProfiles.Find(id);
if (userprofile == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}

db.UserProfiles.Remove(userprofile);

try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}

return Request.CreateResponse(HttpStatusCode.OK, userprofile);
}

protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}

这是我的带有 jQ​​uery ajax 调用的 View :

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<input type="button" id="postBtn" value="Post"/>
<script src="../../Scripts/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
var testPost = (function () {
return {
usePost: function (e) {
//e.defaultPrevented();
$.ajax({
contentType: 'application/json; charset=utf-8',
method: "POST",
url: "/api/Default1",
data: { UserId: 1000, UserName: "Vlado", Email: "vlado@mysite.com", Phone: "(02) 1212121", NameAndSurname: "Vlade Edalv" },
dataType: "json",
beforeSend: function (jqXHR, settings) {
console.log("Sending data...");
},

success: function (data, textStatus, jqXHR) {
console.log("status: " + textStatus + ", data: " + JSON.stringify(data));
},

error: function (jqXHR, textStatus, errorThrown) {
console.log("Status: " + textStatus + ", Error: " + errorThrown);
}
});
},

pageReady: function () {

}
};
} ());

$.ready(testPost.pageReady);
$("#postBtn").on('click', testPost.usePost);
</script>

我面临的问题是,在浏览器网络选项卡中,我看到此调用仍然使用 GET 方法而不是 POST,因此我获取 UserProfile 表中列出的所有数据作为对此 GET 请求的响应。

我知道问题出在路由上,但是不知道该加在哪里,加什么。有人可以告诉我如何使用我的 Api Controller POST 方法吗?

最佳答案

使用类型:而不是方法。 AFAIK 方法已被弃用。

关于javascript - 我的 ajax 调用应该是什么样子 - jQuery、MVC4?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44069494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com