无法获取张贴在PHP FormData [英] Unable to get posted FormData in php

查看:99
本文介绍了无法获取张贴在PHP FormData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP中的AJAX调用来使用发布的FormData,但无法检索变量. 我在做什么错了?

I'm trying to use posted FormData form an AJAX call in PHP, but I cannot retrieve the variables. What I'm doing wrong?

这是我的Javascript

here is my Javascript

var sendData = new FormData();
sendData.append('itemid',$('select#selectItems').val());
sendData.append('itemtitle',$('#item-title').val());
sendData.append('itemtext',$('#item-text').val());

$.ajax({
    url: 'ajax.file.php',
    type: 'POST',
    dataType: 'text',
    data: sendData,
    cache: false,
    contentType: false,
    processData: false
});

和我的PHP

$itemid = $_POST['itemid'];
echo $itemid;

在PHP中始终是未定义的!

is always undefined in PHP!

如果我print_r($ _POST);

and if I print_r ($_POST);

如果我使用Firefox,则PHP文本为:

If I use Firefox the PHP text is:

Array([----------------------------- 12850217581176488271638880149 Content-Disposition:_form-data; _name] =>"itemid "99 ----------------------------- 12850217581176488271638880149内容处置:form-data; name =" itemtitle标题- -------------------------- 12850217581176488271638880149内容处置:form-data; name ="itemtext"文本-------- --------------------- 12850217581176488271638880149--)

Array ( [-----------------------------12850217581176488271638880149 Content-Disposition:_form-data;_name] => "itemid" 99 -----------------------------12850217581176488271638880149 Content-Disposition: form-data; name="itemtitle" The Title -----------------------------12850217581176488271638880149 Content-Disposition: form-data; name="itemtext" The Text -----------------------------12850217581176488271638880149-- )

...并且使用Chrome,PHP响应为:

...and using Chrome the PHP response is:

Array([------ WebKitFormBoundarypyFlwBB31gVYXxRP Content-Disposition:_form-data; _name] =>"itemid" 99 ------ WebKitFormBoundarypyFlwBB31gVYXxRP Content-Disposition:form-data; name ="itemtitle"标题------ WebKitFormBoundarypyFlwBB31gVYXxRP内容处置:form-data; name ="itemtext"文本------ WebKitFormBoundarypyFlwBB31gVYXxRP--)

Array ( [------WebKitFormBoundarypyFlwBB31gVYXxRP Content-Disposition:_form-data;_name] => "itemid" 99 ------WebKitFormBoundarypyFlwBB31gVYXxRP Content-Disposition: form-data; name="itemtitle" The Title ------WebKitFormBoundarypyFlwBB31gVYXxRP Content-Disposition: form-data; name="itemtext" The Text ------WebKitFormBoundarypyFlwBB31gVYXxRP-- )

谢谢

推荐答案

我遇到了同样的问题.当我没有设置Content-Type属性(我使用javascript)时,它就起作用了.

I had the same problem. When I did not set the Content-Type property(I used javascript), it worked.

然后浏览器显示(在使用Chrome调试的请求标头中):
内容类型:多部分/表单数据; boundary = ---- WebKitFormBoundarygfXi4NfQAXa8g7PU

The browser then showed(in the Request Header using Chrome debugging):
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarygfXi4NfQAXa8g7PU

和一个var_dump($ _ POST)向我展示了我想要的东西:
array(2){["user"] => string(4)"test" ["pwd"] => string(7)"testpwd"}

and a var_dump($_POST) showed me what I wanted:
array(2) { ["user"]=> string(4) "test" ["pwd"]=> string(7) "testpwd" }

这篇关于无法获取张贴在PHP FormData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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