简体   繁体   English

无法捕获通过Jquery Ajax发送的$ _POST / $ _ GET

[英]Can't catch $_POST/$_GET sent via Jquery Ajax

See @jpossi Answer. 参见@jpossi答案。

I am not accepting an answer just yet, so that somebody can shed some light on what might be the problem, although it's far fetched as the original POST code is not present. 我现在还没有接受答案,因此,尽管原始的POST代码不存在,但还是很困难,因此有人可以弄清楚可能是什么问题。

Alright, so this worked, I just had to comment out processData: false . 好了,这样就行了,我只需要注释掉processData: false But since the actual function uses the POST method, I tried going back to the original code : var data = new FormData ( ); 但是由于实际函数使用POST方法,所以我尝试返回原始代码: var data = new FormData ( ); keep the processData: false and change to method : 'GET' and guess what, it worked... I simple got back to the original POST code, uncommenting a few lines and commenting out a few lines. 保留processData: false并更改为method : 'GET'并猜猜它起作用了...我简单地回到原始POST代码,取消注释了几行并注释了几行。 what's going on ? 这是怎么回事 ?


Here is the code, it's pretty simple but somehow I am not able to catch the $_POST data sent by the Ajax function. 这是代码,它很简单,但是以某种方式我无法捕获Ajax函数发送的$ _POST数据。 Infact, I am not able to send the correct post data. 实际上,我无法发送正确的帖子数据。

Here I tried with GET option and here is the result. 在这里,我尝试使用GET选项,这是结果。

/**Javascript**/
var data = new FormData (  );
data.append ( 'unique_id',  unique_id  ); // This I checked, it is correct.
$.ajax ( {
    method : 'GET', url : scriptUrl, data : data, cache : false, processData: false, contentType: false, dataType : 'json',
    success : function ( data, textStatus, jqXHR )
    {
        if ( typeof data.error === 'undefined' ) { alert ( data ); }
        else { alert ( 'cccsdsd' ); }
    },
    error : function ( jqXHR, textStatus, errorThrown )
    {
        alert ( textStatus );// This fires with Parseerror.
    }
} );

/**PHP**/
if ( $this->input->get ( 'unique_id' ) ) // I am working with codeigniter.
{
    $data ['message'] = 'My Message';
    echo json_encode ( $data );
}
else 
{
    echo 'Something Else';
}

The Ajax never is successful, it always throws the parseerror . Ajax永远不会成功,它总是会引发parseerror

The firebug GET url turns out like this : http://localhost/mysite/Cart [object%20FormData]&_=1451738500443 萤火虫的GET网址显示如下: http://localhost/mysite/Cart [object%20FormData]&_=1451738500443

The response sent by the server is Something Else . 服务器发送的响应是“ Something Else

What am I doing wrong ? 我究竟做错了什么 ?

jQuery.ajax() expected as "data": PlainObject or String or Array 预期作为“数据”的jQuery.ajax():PlainObject或String或Array

FormData can be used for POST-Requests. FormData可用于POST请求。 (This is handled by Browsers, not jQuery). (这是由浏览器而不是jQuery处理的)。 FormData can not be converted to a GET-String, as it is intended to handle cases like File-Uploads. FormData无法转换为GET-String,因为它旨在处理类似File-Uploads的情况。

You should change data to: 您应该将数据更改为:

var data = {'unique_id': unique_id};

or change from GET to POST 或从GET更改为POST

or (as of comments) change processData to true 或者(作为评论)将processData更改为true

instead of 代替

var data = new FormData (  );
data.append ( 'unique_id',  unique_id  ); // This I checked, it is correct.

just use 只是使用

var data = { unique_id : unique_id };

Where 哪里

var data = { index_name : index_value };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM