程序go 2017-06-14 17:43 采纳率: 100%
浏览 13

AJAX $ .POST函数(数据)

I have some trouble to instert a condition inside the function(data). It works for the second condition (i.e. else) but not for the first one. In other words when data="success" in my php, nothing appears in my html file. Furthermore console.log(data) is "ReferenceError: data is not defined " in both cases even if it works for the second condition.

Also in my networks(console) in Firefox I can see the data send from php in the second condition but for the first one it says that the data was not saved.

$errors is an array in php.

My code in ajax:

            $.post('register3.php', {
                username : $("#username").val(),
                password : $("#password").val(),
                passwordconfirm : $("#passwordconfirm").val(),
                email : $("#email").val()

            },
            function(data){
                    if(data=="success"){
                        $("#results").html(data);
                    }

                    else{

                        $("#errors1").html(data.username);
                        $("#errors2").html(data.password);
                        $("#errors3").html(data.email);
                    }

My code in php

  <?php 
if(!empty($errors)){
  header('Content-Type: application/json; Charset=utf-8');
  $data = json_encode($errors);
  echo $data;
}
else {
    $data='success';
    echo $data;
}
  ?>

My code in HTML

    <div class="flash2">
 <ul> 
    <li><div id="errors1"> </div> </li>
    <li><div id="errors2"> </div> </li>
    <li> <div id="errors3"> </div> </li>
    <li> <div id="results"> </div> </li>


  <ul> 
 </div>   
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 AT89C51控制8位八段数码管显示时钟。
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题