2011-11-02 1 views
0

사용자가 json을 통해 주제를 게시하고 볼 수있는 jquery 모바일 앱을 구축 중입니다. 서버 측은 php와 mysql 주입을 사용하여 json 데이터를 잡습니다. 서버 쪽에서 전달 된 값을 확인하고 SQL에 성공적으로 삽입합니다. 문제는 성공 콜백 결과를 실행하지 않는다는 것입니다. "XMLHttpRequest cannot load http://xxxxxxx/newpost.php. Origin null is not allowed by Access-Control-Allow-Origin."을 내가 "Uncaught ReferenceError: SUCCESS is not defined." 여기jquery mobile 및 json 콜백 결과가 표시되지 않습니다.

에 매개 변수에 오류 변경을 dataType: "jsonp"을 추가하면 내 HTML입니다 :

이 오류 로그는 크롬의 콘솔에 표시

<div data-role="content"> 
      <div data-role="content"> 
       <form id="newPostForm"> 
        <div data-role="fieldcontain"> 
         <label for="postTitle"><strong>Post Title:</strong></label> 
         <input type="text" name="postTitle" id="postTitle" value="" /> 

         <label for="postContent"><strong>Post Content:</strong></label> 
         <textarea name="postContent" id="postContent"></textarea> 

         <fieldset class="ui-grid-a"> 
          <div class="ui-block-a"><a href="#indexPage" id="cancel" data-role="button">Cancel</a></div> 
          <div class="ui-block-b"><button data-theme="b" id="submit" type="submit">Submit</button></div> 
         </fieldset> 
         <h3 id="notification"></h3> 
        </div> 
       </form> 
      </div> 
     </div> 

스크립트 :

function resetTextFields() 
     { 
      $("#postTitle").val(""); 
      $("#postContent").val(""); 
     } 

     function onSuccess(data, status) 
     { 
      resetTextFields(); 
      // Notify the user the new post was saved 
      $("#notification").fadeIn(2000); 
      data = $.trim(data); 
      if(data == "SUCCESS") 
      { 
       $("#notification").css("background-color", "#ffff00"); 
       $("#notification").text("The post was saved"); 
      } 
      else 
      { 
       $("#notification").css("background-color", "#ff0000"); 
       $("#notification").text(data); 
      } 
      $("#notification").fadeOut(5000); 
     } 

     $(document).ready(function() { 
      $("#submit").click(function(){ 

       var formData = $("#newPostForm").serialize(); 

       $.ajax({ 
        type: "POST", 
        url: "http://xxxxxxxx/newpost.php", 
        cache: false, 
        data: formData, 
        success: onSuccess 
       }); 

       return false; 
      }); 

      $("#cancel").click(function(){ 
       resetTextFields(); 
      }); 

      $("#refresh").click(function(){ 
       location.reload(); 
      }); 
     }); 

및 여기에 PHP 코드 :

header('Content-type: application/json'); 
    try 
    { 
     $connection = mysql_connect("localhost","user","pass"); 
     mysql_select_db("dbtable", $connection); 

     $postTitle = mysql_real_escape_string($_POST[postTitle]); 
     $postContent = mysql_real_escape_string($_POST[postContent]); 

     mysql_query("INSERT INTO weblogins (postTitle, postContent) VALUES ('$postTitle', '$postContent')"); 
     mysql_close($connection); 
     echo "SUCCESS"; 
    } 
    catch(Exception $e) 
    { 
     echo $e->getMessage(); 
    } 

버튼을 누르면 클라이언트 측에서 아무런 일도 일어나지 않지만 알림이 표시되지 않지만 제출 된 양식은 SQL에 삽입됩니다.

내가 무엇을 놓쳤는가? 감사.

+0

크롬에 대한 테스트 또는 다른 브라우저에서의 동일한 동작입니까? 귀하의 URL에'www'를 추가하거나'http' 즉'/ xxxxxxx/newpost.php'없이 시도하십시오. – Rafay

+0

나는 이것을했으나 동일한 출처 정책 위반 인 경우 그 결과는 –

답변

0
당신은 게시 할 수 없습니다

/현재 하나 ....

그 전화 같은 원본 정책이 아닌 도메인에 AJAX를 사용하여 얻을 ->http://en.wikipedia.org/wiki/Same_origin_policy

당신은 다음 줄을 추가 할 때

dataType: "jsonp", 

끝에 쉼표를 추가 했습니까 ...?

+0

인데 왜 그렇습니까? 주사가 성공적이라고? 이것은 나에게 아이디어를 준다. 그러나 정말로 혼란 스럽다. –

+0

예 "jsonp"다음에 쉼표를 추가했습니다. –

+0

@SaintShann은 JSONP 설정이있을 때 주입을 수행 했습니까? 그 말이 이해가 될 것입니다 - 오류 콜백에 ... 즉 처리했지만 성공을 성공 – ManseUK

관련 문제