Javascript Jquery.ajax无法获取结束参数
我正在为公司内部网上的内容制作区域创建UI。我在以前的项目中创建了中间件API。这是我第一次使用RESTful方法(首先我对Javascript和jquery相当陌生)Javascript Jquery.ajax无法获取结束参数,javascript,c#,jquery,json,ajax,Javascript,C#,Jquery,Json,Ajax,我正在为公司内部网上的内容制作区域创建UI。我在以前的项目中创建了中间件API。这是我第一次使用RESTful方法(首先我对Javascript和jquery相当陌生) $.ajax({ type: "POST", dataType: "json", data: { "GetData" : {"ThingNumber": thingNumber ,"Show": showBool }}, // JS object not string url: "http://localhost
$.ajax({
type: "POST",
dataType: "json",
data: { "GetData" : {"ThingNumber": thingNumber ,"Show": showBool }}, // JS object not string
url: "http://localhost:11422/api/THING/THING/GetThing",
contentType: "application/json; charset=utf-8",
当我从jquery.ajax调用调试本地api时,JSON对象没有在GET方法上正确传递
$.ajax({
type: "POST",
dataType: "json",
data: { "GetData" : {"ThingNumber": thingNumber ,"Show": showBool }}, // JS object not string
url: "http://localhost:11422/api/THING/THING/GetThing",
contentType: "application/json; charset=utf-8",
在C#API中
$.ajax({
type: "POST",
dataType: "json",
data: { "GetData" : {"ThingNumber": thingNumber ,"Show": showBool }}, // JS object not string
url: "http://localhost:11422/api/THING/THING/GetThing",
contentType: "application/json; charset=utf-8",
我可以在正常工作时点击断点,但我似乎无法理解重载中的对象(json)为何为空。我知道这是javascript中的一些东西,我的单元测试工作正常,我的javascript将我带到方法中的断点,我只需要重载JSON对象以实际传入
$.ajax({
type: "POST",
dataType: "json",
data: { "GetData" : {"ThingNumber": thingNumber ,"Show": showBool }}, // JS object not string
url: "http://localhost:11422/api/THING/THING/GetThing",
contentType: "application/json; charset=utf-8",
更新
$.ajax({
type: "POST",
dataType: "json",
data: { "GetData" : {"ThingNumber": thingNumber ,"Show": showBool }}, // JS object not string
url: "http://localhost:11422/api/THING/THING/GetThing",
contentType: "application/json; charset=utf-8",
它位于api端。我将GetThing的重载从(objectjson)更改为([FromUri()]objectjson)。这解决了眼前的问题,但目前只是将JSON对象转换为空白对象
$.ajax({
type: "POST",
dataType: "json",
data: { "GetData" : {"ThingNumber": thingNumber ,"Show": showBool }}, // JS object not string
url: "http://localhost:11422/api/THING/THING/GetThing",
contentType: "application/json; charset=utf-8",
++注意:我不得不将我的post方法重载更改为[FromBody()],而不是[FromUri()],我还没有更新DELETE方法以查看哪个方法使用了ajax设置:
traditional:true,
?所以问题1,你不能用GET
来实现这一点。两种选择
$.ajax({
type: "POST",
dataType: "json",
data: { "GetData" : {"ThingNumber": thingNumber ,"Show": showBool }}, // JS object not string
url: "http://localhost:11422/api/THING/THING/GetThing",
contentType: "application/json; charset=utf-8",
$.ajax({
type: "POST",
dataType: "json",
data: { "GetData" : {"ThingNumber": thingNumber ,"Show": showBool }}, // JS object not string
url: "http://localhost:11422/api/THING/THING/GetThing",
contentType: "application/json; charset=utf-8",
有关说明,请阅读$.ajax
post文档
$.ajax({
type: "POST",
dataType: "json",
data: { "GetData" : {"ThingNumber": thingNumber ,"Show": showBool }}, // JS object not string
url: "http://localhost:11422/api/THING/THING/GetThing",
contentType: "application/json; charset=utf-8",
2) 使用GET
如果使用此方法,则必须将控制器更改为将
json
参数设置为String
,然后使用所选库将其解析为json ajax
请求中的data
是一个字符串。将其更改为js对象。或者使用JSON.stringify
,然后在服务器上解析它。我还没有尝试过(也不知道该设置是什么),但似乎问题实际上是在api方面。我需要返回数据,这个函数似乎适合GET,当我将其作为POST时,它甚至没有连接到我的api,这给了我500个错误。现在我有了它,我唯一的问题是数据不起作用。根据你之前的建议,我确实将字符串更改为JSON.stringify({“GetData”:{“ThingNumber”:ThingNumber,“Show”:showBool}}),但我不明白为什么我必须使用POST而不是GET。特别是因为我有另一个POST函数,只有GET在工作。除了从字符串更改数据和您将表单POST更改为GET外,其他一切看起来都一样,我是否遗漏了什么?没有办法将数据传递给GET函数吗?没有,您不能将数据作为对象传递给GET
方法,GET
只能发送字符串。@davidp04查看我的编辑我也添加了使用GET的方法。我仍然建议您使用POST
,因为框架可以为您进行转换,所以看起来要容易得多。但是如果您想手动进行字符串化和解析,请继续使用GET
,它仍然可以工作哦,我明白了,这是有道理的。
$.ajax({
type: "POST",
dataType: "json",
data: { "GetData" : {"ThingNumber": thingNumber ,"Show": showBool }}, // JS object not string
url: "http://localhost:11422/api/THING/THING/GetThing",
contentType: "application/json; charset=utf-8",