doujiepin5547 2013-04-14 16:14
浏览 39

PHP页面没有收到jQuery / Ajax POST vars

I have a very simply registration form. The form is sent with jQuery/Ajax to a PHP page for processing. I use this identical code everywhere throughout my app but for some reason, on this one page, it's just not working and I'm trying to figure out where the problem lies. There's nothing special on this page and it's on the same domain so I'm at a loss here...

When the form is submitted, the following jQuery function processes the form:

var dataString = $(event.target).serialize();
var url = '/post/register.php';

console.log( dataString ); // OUTPUTS firstname=Andrew&email=me@email.com

$.ajax({
        type: "POST",
    url: url,
    data: dataString,
    dataType: "text",
    error: function(jqXHR,textStatus,errorThrown) {
        // ERROR
    },
    success: handleRegistration
});

The console shows that the data from the form is properly serialized.

On the PHP page (register.php), I simply have the following code:

$newArr[]= array('email'=>$_POST['email'], 'firstname'=>$_POST['firstname']);

ob_start();
print_r($newArr);   
$c=ob_get_contents();
ob_end_clean();             
$fp=fopen('test.txt', "wb");
fwrite($fp, $c);
fclose($fp);    

This just writes the POST variables to a TXT file called "test.txt". The text file shows the variables are empty:

Array
(
    [0] => Array
        (
            [firstname] => 
            [email] => 
        )

)

I can't for the life of my figure out why it's not receiving the POST vars.

Anyone?

* ANSWER * I changed the url above for demo purposes but in my app the url was:

var url = '/post/register';

and there was an "index.php" file inside the register folder.

The form was getting sent to the PHP page BUT because I was missing the trailing '/', the querystring was not getting sent correctly. So the answer to my problem was to simply change the url to:

var url = '/post/register/';

What a dumb bug on my part - that took way too long to figure out :S

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度