javascript
  • json
  • parsing
  • 2012-02-23 3 views -2 likes 
    -2

    어떻게 Json 응답을 제공하는이 URL을 구문 분석 할 수 있습니까?json의 url에서 구문 분석 응답

    $.getJSON('http://url.php?id=chat_init&sess_d='+sessionID+'&user_id='+document.getElementById("user_id").value+ '&to_id='+document.getElementById("to_id").value,function (data) { 
    
         alert(data.status); 
         alert(data.msg); 
         alert(data.room_id); 
    

    응답 : 어떤 도움

    {"status":1,"msg":"chat is initilize","room_id":13} 
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" /> 
        <title></title> 
        <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script> 
        <script type="text/javascript"> 
    //<![CDATA[ 
        var sessionID=null ; 
        function A() { 
         $.getJSON('url.php?id=login&email='+document.getElementById("txtemail").value+'&password='+document.getElementById("txtpassword").value, function (data) { 
          sessionID = data.session_id; 
          alert(data.status); 
          alert(data.msg); 
          alert(data.user_id); 
          alert(sessionID); 
         }); 
        } 
    
        function B() { 
         $.ajax({ 
          type: 'GET', 
          url: 'url.php', 
          dataType: 'json', 
          data: { 
           id: 'chat_init', 
           sess_d: sessionID, 
           user_id: $("#user_id").val(), 
           to_id: $("#to_id").val() 
         },success: function(data) { 
          alert(data.status); 
          alert(data.msg); 
          alert(data.room_id); 
         } 
        }); 
    
        //]]> 
        </script> 
    
    
    </head> 
    <body> 
        Username : 
    
        <form> 
         <input type="text" id="txtemail" /> 
         <br /><br /> 
    
         Password : <input type="password" id="txtpassword" /> 
         <br /> 
    
         <input type="button" value="Login" onclick="A()" /> 
         <br /> 
    
         My id is : <input type="text" id="user_id" /> 
         <br /><br /> 
    
         To ID is : <input type="text" id="to_id" /> 
         <br /><br /> 
    
         <textarea rows="10" cols="25"></textarea> 
         <br /> <br /> 
    
         <textarea rows="2" cols="18"></textarea> 
    
         <input type="button" value="Initialize chat" onclick="B()" /> 
    
        </form> 
    </body> 
    </html> 
    

    Thankx ... 종류의 추한

    +5

    JSON이 이미 구문 분석됩니다. "이 URL을 파싱하시오"라는 의미는 무엇입니까? * "JSON 구문 분석"*을 의미합니까? - 나는 구글처럼 들린다. – Joseph

    +0

    예 응답을 얻지 못했습니다. –

    +0

    구문을 확인하십시오. – Joseph

    답변

    0
    <input type="button" value="Initialize chat" id="B" /> <!--note the id--> 
    
    $(document).ready(function() { 
    
        //using the button with id "B" to call beta 
        $('#B').on('click', function() { 
         beta(); 
        }); 
    
        function beta() { 
         $.ajax({ 
    
          //base url 
          url: 'url.php', 
    
          //parameters 
          data: {   
           id: 'chat_init', 
           sess_d: sessionID, 
           user_id: $("#user_id").val(), 
           to_id: $("#to_id").val() 
          }, 
    
          type: 'GET', 
          dataType: 'json', 
          success: function(data) { 
           alert(data.status); 
           alert(data.msg); 
           alert(data.room_id); 
          } 
         }); 
        } 
    
    });​ 
    

    http://jsfiddle.net/g3zUe/

    1

    가하는 $.ajax 전화로 다시 작성하려고 :

    $.ajax({ 
        type: 'GET', 
        url: 'url.php', 
        dataType: 'json', 
        data: { 
         id: 'chat_init', 
         sess_d: sessionID, 
         user_id: $("#user_id").val(), 
         to_id: $("#to_id").val() 
        }, 
        success: function(data) { 
         alert(data.status); 
         alert(data.msg); 
         alert(data.room_id); 
        } 
    }); 
    
    +0

    http : //url.php? id = chat_init & sess_d = & user_id = & to_id = & title = 이것은 내 실제 URL입니다. –

    +0

    아니요, 응답을받지 못했습니다 –

    +0

    수동으로 URL을 입력 해 보셨습니까? 너 콘솔을 확인해 봤어? 서버가 JSON을 반환하고 있습니까? – Joseph

    0

    HTML 및 jQuery 코드를 업데이트했습니다. 내 마지막에 그것을 테스트하고 indeed make the AJAX call,하지만 이후로 내 서버에 url.php 없어, 그것은 404 오류를 가져옵니다. 이 기능이 어떻게 작동하는지 확인하십시오.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
        <title></title> 
        <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script> 
        <script type="text/javascript"> 
    //<![CDATA[ 
        var sessionID = null; 
        function A() { 
         $.getJSON('url.php', { 
          id: 'login', 
          email: $("#txtemail").val(), 
          password: $("#txtpassword").val() 
         }, function (data) { 
          sessionID = data.session_id; 
          alert(data.status); 
          alert(data.msg); 
          alert(data.user_id); 
          alert(sessionID); 
         }); 
        } 
    
        function B() { 
         $.ajax({ 
          type: 'GET', 
          url: 'url.php', 
          dataType: 'json', 
          data: { 
           id: 'chat_init', 
           sess_d: sessionID, 
           user_id: $("#user_id").val(), 
           to_id: $("#to_id").val() 
          }, 
          success: function(data) { 
           alert(data.status); 
           alert(data.msg); 
           alert(data.room_id); 
          }, 
          error: function(jqXHR, textStatus, errorThrown) { 
           alert("Error: " + errorThrown); 
          } 
         }); 
        } 
    
        $(document).ready(function() { 
         $("#loginButton").click(A); 
         $("#initChatButton").click(B); 
        }); 
    
        //]]> 
        </script> 
    
    
    </head> 
    <body> 
        <form> 
         <p>Username : <input type="text" id="txtemail" /></p> 
         <p>Password : <input type="password" id="txtpassword" /></p> 
         <p><input type="button" value="Login" id="loginButton" /></p> 
    
         <p>&nbsp;</p> 
    
         <p>My id is : <input type="text" id="user_id" /></p> 
    
         <p>To ID is : <input type="text" id="to_id" /></p> 
         <p><textarea rows="10" cols="25"></textarea></p> 
    
         <p><textarea rows="2" cols="18"></textarea></p> 
    
         <p><input type="button" value="Initialize chat" id="initChatButton" /></p> 
        </form> 
        </body> 
    </html> 
    
    관련 문제