我如何解析这个给出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>
感谢任何帮助...
答案 0 :(得分: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);
}
});
答案 1 :(得分: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);
}
});
}
});
答案 2 :(得分: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> </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>