如何在ajax中使用dataType?

时间:2015-02-05 18:25:04

标签: php html ajax

如何在向服务器发送数据时使用ajax中的dataType并且返回值为echo json_encode("1"),而不是json,请检查以下代码以获取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);
                       }
                    });

和我的PHP代码:

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

我的错误是:

enter image description here

我在谷歌搜索过我找不到任何完美答案。

1 个答案:

答案 0 :(得分:1)

在php中你可以写:

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

在javascript中:

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

希望有所帮助:)