2013-09-16 29 views
0

verify.phpjQuery的阿賈克斯獲得外部鏈接錯誤

<?php 
header('Content-type: application/json'); 
include 'config.php'; 
$con = mysqli_connect($db_host, $db_user, $db_pwd, $database); 
$key1 = $_GET['key1']; 
$name = $_GET['name']; 
$email = $_GET['email']; 
$phone = $_GET['phone']; 
$address = $_GET['address']; 
$installations = mysql_query("SELECT * FROM installations where Key1 = '$key1'"); 
if ($installations !== FALSE && mysql_num_rows($installations) > 0) { 
    while ($row = mysql_fetch_array($installations)) { 
     $key2 = $row['Key2']; 
     $status = $row['Status']; 
     if ($status == 0) { 
      $sql = "UPDATE installations SET Name = '$name', Email = '$email', Address = '$address', Phone = '$phone',Status = '1' WHERE Key1 = '$key1'"; 
      mysqli_query($con, $sql); 
      $data = array('key1' => $key1, 'key2' => $key2); 
      echo(json_encode($data)); 
     } else { 
      $data = array('key1' => 'key not valid', 'key2' => 'key not valid'); 
      echo(json_encode($data)); 
     } 
    } 
} else { 
    $data = array('key1' => 'key not valid', 'key2' => 'key not valid'); 
    echo(json_encode($data)); 
} 
?> 

我的腳本

jQuery.ajax({ 
    type : "GET", 
    url : "http://example.com/verify.php", 
    data : { 
     name : name, 
     email : email, 
     address : address, 
     phone : phone, 
     key1 : key1 
    }, 
    cache : false, 
    success : function(data) { 
     jQuery.ajax({ 
      type : "GET", 
      url : "admin/check_verification.php", 
      data : { 
       key1 : data.key1, 
       key2 : data.key2 
      }, 
      cache : false, 
      success : function(response) { 
       $('.message_outer').fadeIn('slow').html(response); 
       window.setTimeout(function() { 
        location.reload(); 
       }, 2000); 
      } 
     }); 
    }, 
    error : function(jqXHR, exception) { 
     if (jqXHR.status === 0) { 
      alert('Not connect.\n Verify Network.'); 
     } else if (jqXHR.status == 404) { 
      alert('Requested page not found. [404]'); 
     } else if (jqXHR.status == 500) { 
      alert('Internal Server Error [500].'); 
     } else if (exception === 'parsererror') { 
      alert('Requested JSON parse failed.'); 
     } else if (exception === 'timeout') { 
      alert('Time out error.'); 
     } else if (exception === 'abort') { 
      alert('Ajax request aborted.'); 
     } else { 
      alert('Uncaught Error.\n' + jqXHR.responseText); 
     } 
    } 
}); 

我 '無法連接,驗證網絡' 的錯誤。但數據(名稱,電子郵件,地址,電話,key1)保存在我的服務器數據庫中。我用我的本地主機成功測試了這段代碼。我該如何解決這個錯誤。請幫幫我。

+0

你可以發佈'verify.php'的代碼嗎? – Pascamel

+4

您正在與同源政策作鬥爭。 – SLaks

+1

該代碼是否也在'http:// example.com'(同一個域)上運行? –

回答

0

我修復此錯誤,將header("Access-Control-Allow-Origin: *"); 添加到我的服務器verify.php .all謝謝@SLaks。這裏的代碼http://enable-cors.org/server_php.html

<?php 
header("Access-Control-Allow-Origin: *"); 
header('Content-type: application/json'); 
include 'config.php'; 
$con = mysqli_connect($db_host, $db_user, $db_pwd, $database); 
$key1 = $_GET['key1']; 
$name = $_GET['name']; 
$email = $_GET['email']; 
$phone = $_GET['phone']; 
$address = $_GET['address']; 
$installations = mysql_query("SELECT * FROM installations where Key1 = '$key1'"); 
if ($installations !== FALSE && mysql_num_rows($installations) > 0) { 
    while ($row = mysql_fetch_array($installations)) { 
     $key2 = $row['Key2']; 
     $status = $row['Status']; 
     if ($status == 0) { 
      $sql = "UPDATE installations SET Name = '$name', Email = '$email', Address = '$address', Phone = '$phone',Status = '1' WHERE Key1 = '$key1'"; 
      mysqli_query($con, $sql); 
      $data = array('key1' => $key1, 'key2' => $key2); 
      echo(json_encode($data)); 
     } else { 
      $data = array('key1' => 'key not valid', 'key2' => 'key not valid'); 
      echo(json_encode($data)); 
     } 
    } 
} else { 
    $data = array('key1' => 'key not valid', 'key2' => 'key not valid'); 
    echo(json_encode($data)); 
} 
?> 
+1

換句話說,你又犯了一個安全漏洞(除了你現有的SQL注入問題)。現在,其他網站可以使用用戶的憑據讀取數據。 – SLaks