普通网友 2015-02-05 18:25
浏览 58

如何在ajax中使用dataType?

How to use dataType in ajax when you sending data to server and returning value is echo json_encode("1") which is text not json, please check the below code for ajax:

 $.ajax({
                       type: "GET",
                       url: "http://www.pzishkstan.com/MobileApps/controller.php?city="+$city+"&disease="+$disease+"&name="+$name
                       crossDomain: true,
                       contentType: "application/x-www-form-urlencoded; charset=utf-8",
                       dataType: "json",
                       data:'type=register',
                       success: function(data) {


                           if(data=="1"){

                                alert("yes");

                           }else{
                              alert("no");
                           }
                       },
                       error: function(e) {
                           alert('Error: ' + e.message);
                       }
                    });

and my php code:

    $name=$_GET["name"];
                  $city=$_GET["city"];
                  $disease=$_GET["disease"];
                  $birth=$_GET["birth"];
                  $start=$_GET["start"];
                  $graduate=$_GET["graduate"];
                  $email=$_GET["email"];
                  $mobile=$_GET["mobile"];
                  $pass=$_GET["pass"];
                  $web=$_GET["web"];
                  $certi=$_GET["certificate"];
                  $social=$_GET["social"];
                  $uni=$_GET["uni"];
                  $note=$_GET["note"];
                  $gender=$_GET["gender"];

            $explode = explode('/', $_GET["image"]); 

           $image="upload/".array_pop($explode).".png"; 



 $queryNumber="INSERT INTO doctors (Name,Birthdate,StartDate,GraduateDate,Mobile,Email,password,proPic,Website,Gender,Certifications,Socials,City,
  GraduationUniversityName,Notes,regDate) values('$name','$birth','$start','$graduate','$mobile','$email','$pass','$image','$web','$gender','$certi','$social','$city','$uni','$note',
  CURRENT_TIMESTAMP)";

                    $numberOfRows = mysqli_query($link,$queryNumber) or die(mysql_error());


                    if($numberOfRows>0){

                        $query=mysqli_query($link,"select * from doctors where Mobile='$mobile'");
                        while($row=mysqli_fetch_array($query)){
                            $id=$row["DoctorsId"];
                            mysqli_query($link,"INSERT INTO specializationdoctor (doctorId,specializationId) values($id,$disease)");
                        }

                       echo json_encode(array("response" => "1"));


                    }
                else{
                        echo json_encode(array("response" => "0"));
                    }

My error is :

enter image description here

I have been searched in google I couldn't find any perfect answer.

  • 写回答

1条回答 默认 最新

  • from.. 2015-02-05 18:35
    关注

    in php you can write:

      if(....){
                  echo json_encode(array("name" => "yair", "email" => "yair@yair.com"));
                }
            else{
                 echo json_encode(array("name" => "unknown", "email" => "unknown@unknown.com"));
                }
    

    in javascript:

    ....
    success: function(e){
         alert('Your name is '+e.name);
         alert('Your email is '+e.email);
         else .....
    }
    

    hope that helps :)

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀