2012-02-21 197 views
-2

任何人都可以请告诉我为什么我的函数B()不给予回应?解析Json响应

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript"> 
    var sessionID=null ; 
    function A() { 
     $.getJSON('http://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() { 
     $.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); 
       }); 
    } 
</script> 
</head> 
<body> 
Username : <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()"> 

</body> 
</html> 

这是我的代码

+0

请使用合适的标签来显示您的代码 – adis 2012-02-21 12:20:45

+2

请...先对您的标记进行一些操作 – 2012-02-21 12:21:25

+0

我无法做到这一点。否则它表示代码没有正确缩进。 – 2012-02-21 12:24:10

回答

0

使用此功能时,你应该有一个相对路径,而不是完整的。

这意味着http://url.php应该是youscript.php。我也会使用ajax函数而不是getJSON,但这只是一个口味问题。

+0

这就是我的客户的要求。 – 2012-02-21 12:35:02

+0

使用完整的URL或getJSON?该脚本与您的js文件位于相同的位置吗?您不能跨域使用这些功能。 'ajax' /'getJSON'是一样的,我只是想更多地控制,因此赞成'ajax'函数。 – OptimusCrime 2012-02-21 13:18:32

0

'http://url.php'不是有效的URL。

使用相对(“url.php”),绝对(“/url.php”)或完全限定(“http://www.example.com/url.php”)URL。

UPDATE

http://url.php不会在公共网站上工作。

浏览器将尝试将url.php解析为IP地址,其中失败。

+0

除非完全合格的人必须来自同一台服务器,除非您已经实施了CORS – mplungjan 2012-02-21 12:48:53

+0

@SONALKASLIWAL con,否则您确认'url.php'只是一个占位符,并且确实包含有效的URL?我假设,因为功能'A'的作品就是这样。 – Craig 2012-02-21 12:49:35

+0

@ Craig:网址是完美的。 – 2012-02-21 12:50:28