2014-09-09 2 views
0

언제든지 "오류"가 반환됩니다.PHP는 json에서 보낸 json 보낸 데이터를 확인하거나 가져올 수 없습니다.

if($_POST){ 
    $return="Success."; 
    echo json_encode($return); 
} 

jQuery를 : 내 코드 PHP의 index.php라는 하나 개의 파일에

function Save(){ 
       $.ajax({ 
        url: "./index.php", 
        type: "POST", 
        data: { 
         example:"example", 
         example2:$(".content").text() 
        }, 
        dataType: "json" 
       }).done(function(data){ 
        $('#response').append('<p style="color:red;">'+data+'</p>'); 
        alert("done"); 
       }).fail(function(){ 
        $('#response').append('<p style="color:red;">Error</p>'); 
       }); 
      } 

이것은 요청 된 네트워크 탭을 내가 모두가 POST와 OK라고 생각합니다. 네트워크 탭 :

"Success."<!DOCTYPE html> 
<html> 
    <head> 
     <title>Text</title> 
     <script src="style/jquery.min.js"></script> 
     <script> 
     $(document).ready(function() { 

      function Save(){ 
       $.ajax({ 
        url: "./index.php", 
        type: "POST", 
        data: { 
         filename:"something.php", 
         text:$(".content").text() 
        }, 
        dataType: "json" 
       }).done(function(data){ 
        $('#response').append('<p style="color:red;">'+data+'</p>'); 
        alert("done"); 
       }).fail(function(){ 
        $('#response').append('<p style="color:red;">Error</p>'); 
       }); 
      } 

      $(".content").focus(function(){ 
       $(window).bind('keydown', function(event) { 
        if (event.ctrlKey || event.metaKey) { 
         switch (String.fromCharCode(event.which).toLowerCase()) { 
          case 's': 
          event.preventDefault(); 
          Save(); 
          break; 
         } 
        } 
       }); 
      }); 
     }); 
     </script> 
     <style> 
      .content { 
       width: 80%; 
       height: 80%; 
       border: 1px solid #ccc; 
       padding: 5px; 
      } 
     </style> 
    </head> 
    <body> 
     <div id="response"></div> 
     <pre><div class="content"> 
&lt;?php 
    if(true){ 
     echo 'ds'; 
     //das 
    } 
.test { 
    size: 5pt; 
} 
     </div></pre> 
    </body> 
</html> 

가 어떻게이 문제를 해결하는 방법을 말해 줄래 : 여기

Remote Address:127.0.0.1:80 
Request URL:http://localhost/test/test/test/index.php 
Request Method:POST 
Status Code:200 OK 
Request Headersview source 
Accept:application/json, text/javascript, */*; q=0.01 
Accept-Encoding:gzip,deflate 
Accept-Language:en-US,en;q=0.8,bg;q=0.6 
Connection:keep-alive 
Content-Length:606 
Content-Type:application/x-www-form-urlencoded; charset=UTF-8 
Cookie:_ga=GA1.1.1569762264.1406894118 
Host:localhost 
Origin:http://localhost 
Referer:http://localhost/test/test/test/ 
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36 
X-Requested-With:XMLHttpRequest 
Form Dataview sourceview URL encoded 
example:example 
example2: 
<?php 
    if(true){ 
     echo 'ds'; 
     //das 
    } 
.test { 
    size: 5pt; 
} 

Response Headersview source 
Connection:Keep-Alive 
Content-Length:1641 
Content-Type:text/html 
Date:Tue, 09 Sep 2014 20:03:46 GMT 
Keep-Alive:timeout=5, max=97 
Server:Apache/2.4.9 (Unix) OpenSSL/1.0.1g mod_fcgid/2.3.9 PHP/5.5.11 mod_perl/2.0.8-dev Perl/v5.16.3 
X-Powered-By:PHP/5.5.11 

그리고는 네트워크 탭에서 응답 탭입니까?

+1

이 오류가 발생하면 브라우저 도구를 살펴보고받은 응답을 살펴보고 문제가 어디에 있는지 알려줍니다. – Hammerstein

답변

0

클라이언트가 JSON을 기다리고 있으면 JSON 이외의 다른 것을 인쇄 할 수 없습니다. 당신은 그 후 스크립트를 종료해야합니다 당신은 PHP 스크립트의 실제 결과를 볼 수 있도록

if($_POST){ 
    $return="Success."; 
    echo json_encode($return); 
    exit(); 
} 

?> 
<!DOCTYPE html> 
<html> 
... 
+0

그들을 제거하고 다시 "오류"를 반환합니다. – emanuilov

+0

'json_decode' 호출이 실패하고 인쇄 오류 메시지 일 수 있습니다. Developer Tools/Firebug의 Network 탭에서 전체 JSON 응답을 확인하십시오. – Barmar

+0

좋은 지적 @Barmar – ShayneStatzell

0
$return = json_encode(array('success')); 
echo $return; 
+0

그의'.done()'함수는 응답을 배열로 기대하지 않습니다. 그가 이것을한다면, Javascript에서'data [0]'으로 변경해야합니다. – Barmar

0

는 양식 제출을 통해 POST 요청을 할 수있는 더미 양식을합니다. 추가로

ini_set('display_errors',1); 
error_reporting(E_ALL); 

을 PHP 페이지 상단에 추가하면 PHP에서 발생한 오류를 표시 할 수 있습니다. 입력 된 페이지를 실행하십시오. 예상 한 결과를 얻을 때까지 오류를 수정하십시오. 그런 다음 자바 스크립트가있는 페이지로 돌아가서 제대로 작동하는지 확인하십시오.

+0

나는 그것을 덧붙여 어떤 종류의 오류,주의 사항도 나오지 않았다. – emanuilov

+0

PHP 스크립트가 어떤 오류도 발생시키지 않으면 AJAX 호출에 문제가 있어야한다. – HamHamJ

관련 문제