ajax发送put请求参数,请求参数和PUT方法(Request params and PUT method)

请求参数和PUT方法(Request params and PUT method)

我试图通过基于Grails的应用程序获取PUT请求传递的请求参数。

我正在使用以下客户端代码发出请求:

$.ajax({

url: 'api/controllerName/anId',

type: 'PUT',

data: $('form').serialize()

})

与以下映射:

"/api/$controller/$id?" {

action = [ GET: "read", POST: "create", PUT: "update", DELETE: "delete"]

}

但我的控制器的操作收到空的参数列表,只有id值。 我试图把它的内容放在日志中,只看到:

[id:anId, action:[GET:read, POST:create, PUT:update, DELETE:delete], controller:controllerName]

request.getParameterNames()返回值的空值列表。

正如我从FireBug看到的,请求包含这个参数,并且将Content-Type设置为application/x-www-form-urlencoded; charset=UTF-8 application/x-www-form-urlencoded; charset=UTF-8

如果我使用GET / POST方法 - 一切都按预期工作,我可以获取所有传递的参数。

我如何获得传递的参数?

更新 :我刚才发现PUT意味着将数据作为JSON / XML传递给body。 顺便说一句,这个问题仍然是实际的,以防万一

I'm trying to get request params passed by PUT request, at Grails-based app.

I'm using following client code to make request:

$.ajax({

url: 'api/controllerName/anId',

type: 'PUT',

data: $('form').serialize()

})

with following mapping:

"/api/$controller/$id?" {

action = [ GET: "read", POST: "create", PUT: "update", DELETE: "delete"]

}

But my controller's action receives empty params list, with only id value. I tried to put it content to logs and saw only:

[id:anId, action:[GET:read, POST:create, PUT:update, DELETE:delete], controller:controllerName]

and request.getParameterNames() returns empty list of values.

As I see from FireBug, request contains this params, and have Content-Type as application/x-www-form-urlencoded; charset=UTF-8

If I'm using GET/POST method - everything is working as expected, I can get all passed parameters.

How I can get access to passed parameters?

Update: I've just figured that PUT implies passing data as JSON/XML in body. Btw, this question is still actual, just in case

原文:https://stackoverflow.com/questions/7195872

更新时间:2019-11-04 14:25

最满意答案

我今天有完全相同的问题,并能够解决它。 我使用JQuery向Grails RESTful WebService(方法:“PUT”)做了一个ajax请求,但参数映射是空的。

只是想在这里分享我的解决方案。

我必须JSON.stringify我的数据,并将contentType设置为“application / json”,然后在我的Grails控制器中使用request.JSON(如前所述)。

示例(JQuery Ajax请求):

$.ajax({

url: "/entries/" + id,

contentType: "application/json",

type: "PUT",

dataType: "json",

data: JSON.stringify({'name' : name, 'date': date}),

success: function(msg) {

console.log('updated...')

$.mobile.changePage($('#list'));

}

之后我可以使用:

request.JSON

在我的控制器中,以获取由ajax请求发送的数据(参数仍为空)。 如果我没有使用JSON.stringify并设置内容类型,我有以下Grails错误:

Error 2012-04-03 13:50:20,046 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver - IllegalStateException occurred when processing request: [PUT] /entries/2

getReader() has already been called for this request. Stacktrace follows:

Message: getReader() has already been called for this request

我使用Grails 2.0.1和JQuery 1.7.1

I had the exact same issue today and was able to fix it. I did an ajax request with JQuery to a Grails RESTful WebService (method: "PUT"), but the parameter map was empty.

Just want to share my solution here.

I had to JSON.stringify my data and set the contentType to "application/json" and then use request.JSON (as suggested before) in my Grails controller.

example (JQuery Ajax Request):

$.ajax({

url: "/entries/" + id,

contentType: "application/json",

type: "PUT",

dataType: "json",

data: JSON.stringify({'name' : name, 'date': date}),

success: function(msg) {

console.log('updated...')

$.mobile.changePage($('#list'));

}

afterwards I could use:

request.JSON

in my Controller in order to get the data sent by the ajax request (params was still empty). If I did not use JSON.stringify and set the content type, I had the following Grails error:

Error 2012-04-03 13:50:20,046 [http-bio-8080-exec-3] ERROR errors.GrailsExceptionResolver - IllegalStateException occurred when processing request: [PUT] /entries/2

getReader() has already been called for this request. Stacktrace follows:

Message: getReader() has already been called for this request

I'm using Grails 2.0.1 and JQuery 1.7.1

相关问答

我今天有完全相同的问题,并能够解决它。 我使用JQuery向Grails RESTful WebService(方法:“PUT”)做了一个ajax请求,但参数映射是空的。 只是想在这里分享我的解决方案。 我必须JSON.stringify我的数据,并将contentType设置为“application / json”,然后在我的Grails控制器中使用request.JSON(如前所述)。 示例(JQuery Ajax请求): $.ajax({

url: "/entries/"

...

在这种情况下,不要使用RKRoute 。 当你调用put...时,明确地提供适当的路径。 请注意,您还应该有2个响应描述符,因为路径模式不同。 Just don't use the RKRoute in this case. Explicitly supply the appropriate path when you call put.... Note that you should also have 2 response descriptors because the path patter

...

最后我找到了这个解决方案:在Zend / Controller / Plugin /中你可以找到PutHandler.php。 它可以用于使用$ this-> getRequest() - > param_name来获取PUT参数。 将它包含在你的boostrap.php文件中: protected function _initRestRoute()

{

$this->bootstrap('frontController');

$frontController = Zend_Con

...

我假设你要问的是,如果你可以有这样的方法: def restaction(request, id):

if request.method == "PUT":

someparam = request.PUT["somekey"]

答案是否定的,你不能。 Django不会为PUT,OPTIONS和DELETE请求构建这样的字典, 这里介绍的推理。 为了总结一下,REST的概念是,您交换的数据可能比简单的键映射到值更复杂。 例如,打印图像,或使用json。 一个框架不知道你可能

...

您可以在路由中传递id ,而有效负载中的Actor使用following overload作为第二个参数作为对象: HttpResponseMessage response = client.PutAsJsonAsync("api/Actors/123", new

{

actor = new Actor

{

Name = "John Smith",

Age = 30,

}

});

You could pass the id in the

...

调用资源时,您可以像这样指定urlParams和formBody Params resource(auth).putRequest({id: data.id}, {thing1: new, thing2: old}).$promise

第一个参数是urlParams,第二个是formBody,有关详细信息,请查看文档 When calling a resource you can specify the urlParams and the formBody Params like so reso

...

作为解决方法,我在request.body中找到它们并手动解析编码参数。 这有点烦人,但它确实有效。

new_values = urlparse.parse_qs(self.request.body)

# values show as lists with only one item

for k in new_values:

new_values[k] = new_values[k][0]

As a

...

你说过,你的logcat中出现“ArrayIndexOutOfBound”错误。 可能是你的getParams()方法有问题。 更改for循环: for (int i = 0; i <= tripData.length; i++)

至: for (int i = 0; i < tripData.length; i++)

立即检查,如果您遇到任何其他问题,请在此处显示您的logcat。 You said, getting "ArrayIndexOutOfBound" error in your

...

只做一次改变,它将完美无缺。 params.put("countryid", ""+countryID);

Do only one change, it will work perfect. params.put("countryid", ""+countryID);

我建议将ReorderedTasks [i] .ID.ToString()直接放到url路径。 var request = new RestRequest($"api/task/5/{ReorderedTasks[i].ID.ToString()}/", Method.PUT);

它将有助于减少http请求格式可能出现的问题。 I suggest to put ReorderedTasks[i].ID.ToString() directly to url path. var request =

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值