【发布时间】:2014-07-20 21:56:55
【问题描述】:
我通过 XMLHttpRequest 将 2 个下拉列表值发布到 php,在按下发送按钮后,我可以看到在 firebug 中是可以的。将这些值发送到 php,在 php 中我进行查询并将结果存储到数组中,然后我回显 json_encode ($queryresult)。现在的问题是我试图在第一页中获取该数组,该数组使用 javascript 发送值但没有结果,但我可以通过网络 xhr 响应中的 firebug 看到回显数组:
Html表单:(通过另一个js函数正确填写选择)
<form>
<table style="width:300px">
<h2>Consulta Fichas Técnicas:</h2>
<tr>
<td>Marca:</td>
<td> <select id="se" name="select_marca">
<option value="0"> --Selecciona Marca--</option>
</select></td>
<td>Combustible:</td>
<td><select id="combus" name="combust">
<option value="0"> --Selecciona Combustible-- </option>
</select></td>
<td>
<input id="botonenvia" type="button" value="Enviar Consulta" onClick="sendInfo()"/>
<!-- <input type="submit" />-->
</td>
</tr>
</table>
</form>
<div id="result"></div>
Javascript XHR 部分:
<script type="text/javascript" >
var ajaxWhatcha;
if (window.XMLHttpRequest) ajaxWhatcha = new XMLHttpRequest();
else if (window.ActiveXObject) ajaxWhatcha = new ActiveXObject("Microsoft.XMLHTTP");
function sendInfo() {
ajaxWhatcha.onreadystatechange = function() {
if (ajaxWhatcha.readyState == 4) {
document.getElementById("botonenvia").value = "Enviado ";
}
}
ajaxWhatcha.open("GET", "consulta.php?thingsIsend=" + document.getElementById("se").value +"& thingsIsend2=" + document.getElementById("combus").value, true);
ajaxWhatcha.send(null); // Send it
}
</script>
现在我需要捕捉我试过这个 Jquery 却没有结果的响应,也许我应该用另一个 xhr 以某种方式捕捉它?:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.ajax({
type: "GET",
url: "consulta.php",
contentType: 'application/json; charset=utf-8',
async: false,
dataType: "json",
success: function(data){
//var data = jQuery.parseJSON(data);
document.write('entering success'); //doesnt print
console.log('entering succes');
alert(xhr.status);alert(data); //no alert prints
for (i=0;i<data.length;i++){
alert(data[i].modelo);
}console.log(data);
}
});
</script>
也试过没有结果:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
alert(xhr.responseText);
}
}
Php 只是用查询结果回显 json_encodes 数组。抱歉帖子太长了
【问题讨论】:
-
在访问数据之前尝试像
data = jQuery.parseJSON(data);一样解析成功函数中的JSON。 -
试过了,显然没什么变化。
-
尝试检查控制台,如果有任何响应。
-
console.log=(数据);成功下不打印任何东西
标签: javascript jquery ajax forms