从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个回答



首先你改变</ p>

  print json_encode($ data);

echo $ data;
</ code> </ pre>

To </ p>

  echo json_encode($ data); 
</ code> </ pre>

比使用像这样的ajax直接获得响应的更好方法</ p>

  $ .ajax({
method:“POST”,

url:“call.php”,
dataType:'json',
data:“account_name =”+ accName
})。done(function(data){
createAcct(data);
} );
</ code> </ pre>

现在在 createAcct </ strong>函数中,您将获得响应</ p>

  function createAcct( 数据){
console.log(“f”);
console.log(data);
console.log(data ['ca']);
console.log(typeof data);
}

</ code> </ pre>

您还可以使用ajax success替换done函数</ p>

  success:function(data){
createAcct(data);
}
</ code> </ pre>
</ div>

展开原文

原文

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);
}



传递'Content-Length:'。 strlen($ data))</ p>
</ div>

展开原文

原文

pass 'Content-Length: ' . strlen($data))

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐