将ajax post数据用于PHP文件

时间:2014-08-05 14:18:00

标签: javascript php jquery ajax

我通过$ .ajax调用发送数据到php文件。当ajax调用完成后,我运行另一个在php文件上执行$ .getJSON的javascript函数。

在php文件中是一个变量,它应该具有来自ajax的发布数据的值,但它是空的。

function inputSummonerName() {
    inputName = prompt("summoners name");

    $.ajax({
        type: 'POST',
        url: 'ajax.php',
        data: { n : inputName },
        complete: function() {
            getImageData();
        }
    });
}

function getImageData() {
    $.getJSON('ajax.php', function (json) {
        if(!json){
            console.log("empty");
        }
        else{
            $.each(json, function (index) {
                summonerProfileIconId = json[index].profileIconId;
                summonerName = json[index].name;
                summonerLevel = json[index].summonerLevel;
                $(".summonerName").text(summonerName);
                $(".summonerProfileIconId").attr("src", "http://ddragon.leagueoflegends.com/cdn/4.13.1/img/profileicon/" + summonerProfileIconId + ".png");
                $(".summonerLevel").text("Level" + summonerLevel);
            });
        }
    });
}

这是php文件ajax.php:

<?php
    $apikey = "...";
    if (isset($_POST["n"])) {
        $summonerName = $_POST["n"];
    }

    $data = json_decode(file_get_contents("https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/" . $summonerName . "?api_key=" . $apikey ));

    echo json_encode($data);

    ...
?>

3 个答案:

答案 0 :(得分:2)

您正在向PHP文件发出两个请求。

  1. $.ajax - 从远程服务器获取所需数据并将其返回。
  2. $.getJSON - 再次点击远程服务器(但名称缺失)并返回。
  3. 您正试图从2中获取数据,而这些数据并不存在。不要使用getJSON。只需使用第一个complete函数中的数据即可。更好的是,使用success以便在发生服务器错误时不要尝试使用它。考虑添加一个明确的error处理程序。

    您还应该编写PHP,以便它告诉浏览器它正在发送JSON而不是HTML,并且解码JSON没有任何意义,然后只做重新编码。

    这样:

    header("Content-Type: application/json");
    echo file_get_contents("https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/" . $summonerName . "?api_key=" . $apikey );
    

    然后在JS:

    function inputSummonerName() {
        inputName = prompt("summoners name");
        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: {
                n: inputName
            },
            success: function(a) {
                if (!a) console.log("empty"); else $.each(a, function(b) {
                    summonerProfileIconId = a[b].profileIconId;
                    summonerName = a[b].name;
                    summonerLevel = a[b].summonerLevel;
                    $(".summonerName").text(summonerName);
                    $(".summonerProfileIconId").attr("src", "http://ddragon.leagueoflegends.com/cdn/4.13.1/img/profileicon/" + summonerProfileIconId + ".png");
                    $(".summonerLevel").text("Level" + summonerLevel);
                });
            }
        });
    }
    

答案 1 :(得分:1)

您使用$ .ajax和$ .getJSON(这是$ .ajax的包装器)调用脚本两次。 解决方案:

function inputSummonerName()
{
    inputName = prompt("summoners name");

    $.ajax({
        type: 'POST',
        url: 'ajax.php',
        data: { n : inputName},
        success: function(data, textStatus, jqXHR)
        {
            getImageData($.parseJSON(data));
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            //handle your error!
        }
    });

}

function getImageData(data)
{
    $.each(data, function (index) 
    {
        summonerProfileIconId = data[index].profileIconId;
        summonerName = data[index].name;
        summonerLevel = data[index].summonerLevel;
        $(".summonerName").text(summonerName);
        $(".summonerProfileIconId").attr("src", "http://ddragon.leagueoflegends.com/cdn/4.13.1/img/profileicon/" + summonerProfileIconId + ".png");
        $(".summonerLevel").text("Level" + summonerLevel);
    });
}

我还将完成更改为成功/错误,因此在出错时,其他函数不会因为非数组值而生气。

答案 2 :(得分:-1)

尝试将n放在引号中它应该是键,而不是变量。 IE:

function inputSummonerName(){
inputName = prompt("summoners name");

$.ajax({
    type: 'POST',
    url: 'ajax.php',
    data: { 'n' : inputName},
    complete: function(){
        getImageData();
    }
});

PS:如果你真的想要http://api.jquery.com/jQuery.post/

,还有jquery的简写