普通网友 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 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型