dtvjl64442 2017-02-07 05:56
浏览 56

从php cUrl返回的Ajax没有任何价值

When I click the submit button, there's an ajax post request. But then the returned json value has only "" in it. Even the console.log(data) is not working when the .done() ajax is fired up.

<form id="form" action="#">
    <input type="email" placeholder="email" id="account_name" name="account_name" value="email@test.com"> <br>
    <input type="text" placeholder="First Name" id="first_name"  name="first_name" value="volvo"><br>
    <input type="text" placeholder="Last Name" id="last_name"  name="last_name" value="volva"><br>
    <input type="submit" name="submit" id="btn"/>
</form>

    <script type="text/javascript">
        $(function(){
            var btn = $("#btn");
            var form = $("#form");
            var accName =$("#account_name").val();

            form.on("submit",function(evt){
                evt.preventDefault();
                var myReq = $.ajax({
                    method:"POST",
                    url:"create.php",
                    dataType: 'json',
                    data: "account_name="+accName
                });
                myReq.done(createAcct);
            });

            function createAcct(data){
                console.log(data);
                var a = JSON.parse(data);
                console.log(typeof a);

            }    
        });
    </script>

So the returned json is this {"ac":""}Array in the response tab. Here's my php curl

$ca = createAcct($_REQUEST['account_name']);

$data = ["ca"=>"$ca"];//should be the email
print json_encode($data);
echo $data;

function createAcct($email_account){
    $data = '{"account_name":"'.$email_account.'"}';
    $ch = curl_init();

    //Set cURL parameters to perform API Call
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, "https://api.website.com/api/accounts/create");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    $statusCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);

    $output = curl_exec($ch);
    $err = curl_error($ch);
    if ($statusCode == 204) {
        return $email_account;
        curl_close($ch);
    } else {
        curl_close($ch);
    }

}
  • 写回答

2条回答

  • duanji5116 2017-02-07 06:21
    关注

    First of all you change

    print json_encode($data);    
    echo $data;
    

    To

    echo json_encode($data);
    

    Than it's better way to get response direct with ajax like this

    $.ajax({
        method:"POST",
        url:"call.php",
        dataType: 'json',
        data: "account_name="+accName
    }).done(function(data) {
        createAcct(data);
    });
    

    Now in createAcct function you get response

    function createAcct(data){
        console.log("f");
        console.log(data);
        console.log(data['ca']);
        console.log(typeof data);
    }   
    

    You can also use ajax success replace of done function

    success : function(data) {
        createAcct(data);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算