weixin_33701617 2017-01-07 23:05 采纳率: 0%
浏览 180

AJAX返回空值

I'm trying to get my AJAX request to PHP working. It should, after sending the AJAX request loop through some items and then send back an array of data. The problem is that it returns an empty string.

Here's the code:

PHP

if(isset($_POST['action']) && !empty($_POST['action'])) {

    $available = array();

    for($i = 0; $i <= 50; $i++) {

        $gt = substr(md5(microtime()),rand(0,26),3);

        $check_url = "SOMEURL" . $gt . ".card";
        $check_url_source = file_get_contents( $check_url );

        $exists = ( preg_match( "#<div id=\"Gamerscore\">--</div>#i", $check_url_source ) ) ? false : true;

        if($exists == false) {
            array_push($available, $gt);
        }
    }

    echo json_encode($available, JSON_FORCE_OBJECT);
}

JQuery

$('document').ready(function() {
    $.ajax({
        url: '/GamertagChecker/helpers/magic.php',
        data: {action: 'load'},
        type: 'post',
        success: function(output) {
            console.log(output);
        },
        error: function(jqXHR, exception){
            console.log("Error: " + jqXHR.responseText);
        }
    });
});
  • 写回答

1条回答 默认 最新

  • weixin_33744854 2017-01-08 01:24
    关注

    After looking through a lot of solutions and research by others here I've finally found the problem.

    Because I was using the PHPStorm IDE, I was also using the build-in server. Unfortunately there is a bug in the server which causes $_POST to not work.

    After copying the files to a different IDE I finally got it working. Thanks to everyone who tried to help, your effort is much appreciated!

    评论

报告相同问题?