ajax调用有时会从POST更改为GET [英] ajax call sometimes changes from POST to GET

查看:143
本文介绍了ajax调用有时会从POST更改为GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于用户选择的带有ajax调用的页面.在某些情况下,尽管将其指定为POST,但将其更改为GET;结果,被调用页面中的代码失败.

I have a page with an ajax call based on user choices. In some cases, though it's specified as POST, it gets changed to GET; as a result, the code in the called page fails.

尽管$ _SERVER ['REQUEST_METHOD']告诉我这是一个GET,但在响应页面中,$ _ GET变量为空,就像$ _POST变量一样. (我希望可以代替$ _GET的参数来解决此问题.)

Although $_SERVER['REQUEST_METHOD'] tells me it's a GET, in the response page, the $_GET variable is empty, just like the $_POST variable. (I was hoping I could work around the problem by grabbing the params from the $_GET instead.)

这是ajax调用的相关部分:

Here's the relevant portion of the ajax call:

var filters = getFilters();
var fnargs = "GetArticles|" + filters ;

$.ajax({
    url: 'retrievedata.php',
    type: "POST",
    async:true,
    data: {"functionname":"getpapers", "arguments":fnargs},
    dataType: "JSON",

其后是成功和错误部分.

It's followed by success and error sections.

getFilters函数构建一个逗号分隔的字符串,其中包含3个较短的字符串.它们中的任何一个或全部都可以是空的.填充时,每个都是逗号分隔的整数列表.以下是一些示例:

The getFilters function builds a comma-separated string containing 3 shorter strings. Any one of them or all of them can be empty. When populated, each is a comma-separated list of integers. Here are some examples:

'1','3,6','2015'
'','',''
'1','',''

在每种情况下,Firefox的Web控制台的网络"选项卡都向我显示已为该呼叫组装了正确的参数,并且就此而言,它是POST.但是我在retrievedata.php(被调用的页面)中添加了对$ _SERVER ['REQUEST_METHOD']的检查,发现有些时候POST已成为GET.

In every case, the Network tab of Firefox's Web Console shows me that the correct parameters have been assembled for the call, and for that matter, that it's a POST. But I added a check of $_SERVER['REQUEST_METHOD'] in retrievedata.php (the called page) and I find that some of the time, the POST has become a GET.

在始终以相同的方式对待相同的getFilters结果的意义上,这是一致发生的.但是除此之外,我能找到的唯一一致性是,如果填充了第三个字符串,它将始终有效.在某些情况下,它可以工作:

This happens consistently in the sense that the same getFilters result is always treated the same way. But beyond that, the only consistency I can find is that if the third string is populated, it always works. Here are some cases where it works:

'','','1998'
'','','1998,2008'
'','1',''
'','2,1',''
'','2',''
'1','','1999'
'1','1','1999'
'1','1',''
'1,3','1',''
'3','',''

以下是一些更改为GET的地方:

Here are some where it changes to GET:

'','',''
'','3,1',''
'','2,3',''
'','3',''
'1','',''
'1,3','',''

可能应该注意,我将原始问题发布在 ajax调用中:有时,_POST是空.感谢那里的评论者,我发现对GET的更改是关键问题.

Probably should note that I posted the original problem at ajax call: Sometimes, _POST is empty. Thanks to a commenter there, I figured out that the change to GET was the key issue.

此外,值得一提的是,我还有另一个页面具有几乎相同的调用,除了它的getFilters版本仅连接两个逗号分隔的字符串(并且fnargs的第一个组件是"GetPapers"而不是"GetArticles").总是打正确的电话.

Also, worth adding that I have another page with a virtually identical call, except that its version of getFilters concatenates only two comma-separated strings (and the first component of fnargs is "GetPapers" rather than "GetArticles". That page always makes the right call.

推荐答案

事实证明,似乎从POST切换到GET的症状是红色鲱鱼(可能是由于错误处理程序代码中的问题).真正的问题在于,某些数据返回了json_encode()无法处理的字符.将数据库连接设置为UTF-8可以解决此问题.

It turns out that the symptom of appearing to switch from POST to GET was a red herring (possibly due to issues in the error handler code). The real problem here was that some of the data was coming back with characters that json_encode() couldn't handle. Setting the database connection to UTF-8 fixed it.

这篇关于ajax调用有时会从POST更改为GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆