2016-09-28 2 views
0

MySQL 데이터베이스에 양식의 일부 필드를 삽입해야하는 php 파일에 대해 ajax 호출을하는 함수가 있습니다. 삽입 작업이 제대로 작동하지만 경고에 응답을 인쇄 할 때 함수가 위의 오류를 반환합니다. 누군가 왜 이런 일이 일어날 지 말해 줄 수 있습니까?WebService에 대한 Ajax 요청이 작동하더라도 오류를 반환합니다.

답변 : : 경고에 표시

오류 { "readyState가"0 "상태": 0, "하는 statusText": "오류"}

funcoes.js

function salvar(){ 
$.ajax({ 
    type: 'POST', 
    dataType: 'json', 
    data: {operationType: 'insert', nome: $('#nome').val(), sobrenome: $('#sobrenome').val(), idade: $('#idade').val() }, 
    url: 'http://localhost/projetos/wstest/cadastrar.php', 
    ContentType: 'application/json', 
    success: function(response){ 
     alert('Resposta: '+JSON.stringify(response)); 
    }, 
    error: function(err){ 
     alert('Resposta: '+JSON.stringify(err)); 
     alert('Erro ao inserir registro!'); 
    } 
});} 

cadastrar.php

include './conection.php'; 

header("Access-Control-Allow-Origin: *"); 

$link = conectar(); 

$nome = mysqli_real_escape_string($link, $_REQUEST['nome']); 
$sobrenome = mysqli_real_escape_string($link, $_REQUEST['sobrenome']); 
$idade = mysqli_real_escape_string($link, $_REQUEST['idade']); 

$query = "INSERT INTO `usuario`(`nome`, `sobrenome`, `idade`) " 
    . "VALUES ('$nome','$sobrenome','$idade')"; 

$res = mysqli_query($link, $query); 

if(isset($_POST['operationType'])) 
{ 

    if ($_POST['operatioType'] == "insert") 
{   
    $query = "INSERT INTO `usuario`(`nome`, `sobrenome`, `idade`) " 
      . "VALUES ('$nome','$sobrenome','$idade')"; 

    $res = mysqli_query($link, $query); 

    if ($res == true) 
    { 
     $resultado = 1; 

     return $resultado; 
    } 
    else 
    { 
     $resultado = 0; 

     return $resultado; 
    } 

    echo json_encode($resultado); 
} 

else if($_POST['operationType'] == "login") 
{ 

} 
} 
else 
{ 
    echo "Formato de requisição inválido! O aplicativo não conseguiu se comunicar " 
. "de maneira correta com o servidor."; 
} 
+0

먼저 차단하면에서 선 return $resultado;을 제거 여기 봐 : http://www.justinball.com/2013/02/25/jqxhr-returning-readystate-0 및 상태 -0 /. 둘째 : PHP 코드는 1 또는 0을 반환하고, 쿼리가 두 번 존재하며, 그리고 ... 또 다른 이야기이므로 json 응답을 보내지 않습니다. –

답변

1
  1. 블록 다른 &이

    if ($res == true) 
    { 
        $resultado = 1; 
    
        return $resultado; //remove this line 
    } 
    
관련 문제