[英]div not getting populated by ajax request
i'm trying to make an ajax call with jquery, it seems to be working, but i cant get it to populate the result div. 我正在尝试使用jquery进行ajax调用,它似乎正在工作,但是我无法获取它来填充结果div。
Heres the JS code: 这是JS代码:
<script>
$(document).ready(function(){
$(document).on('change', '#flip-1', function(){
var datastring = $("#some-form").serialize();
$.ajax
({
type: "POST",
url: "test.php",
data: datastring,
dataType: 'html',
complete: function(data) {
$('#results').html (data);
}
});
});
});
</script>
<form id="some-form">
<label for="flip-1">Flip toggle switch:</label>
<select id="flip-1" name="flip-1" data-role="flipswitch">
<option value="off">Off</option>
<option value="on">On</option>
</select>
</form>
<div id="results">Loading users...</div>
and here is the PHP code: 这是PHP代码:
<?php
if ($_POST['flip-1'] == 'on') {
echo 'oh is on!';
} elseif ($_POST['flip-1'] == 'off') {
echo 'no, its not on';
}
?>
this is what the chrome "inspect element" debugger say about headers: 这是Chrome浏览器“检查元素”调试器对标头所说的:
Request URL:http://localhost/smart_home/test.php
Request Method:POST
Status Code:200 OK
Request Headers
POST /smart_home/test.php HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 9
Accept: text/html, */*; q=0.01
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost/smart_home/index.php
Accept-Encoding: gzip,deflate,sdch
Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4
Form Data
flip-1=on
Response Headers
HTTP/1.1 200 OK
Date: Thu, 27 Feb 2014 20:58:08 GMT
Server: Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.6
X-Powered-By: PHP/5.5.6
Content-Length: 20
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html
And this is what the response tab says: when the button changes to the on position: 这就是“响应”选项卡上的内容:当按钮更改为“打开”位置时:
oh is on!
when changed to "off" 更改为“关”时
no, its not on
does anyone have any ideas? 有人有什么想法吗?
thanks! 谢谢!
The complete
callback doesn't receive the response data directly (The first parameter is a jqXHR
object). complete
回调不会直接接收响应数据(第一个参数是jqXHR
对象)。 It's generally simpler to use the success
or .done()
callbacks instead: 通常,使用success
或.done()
回调来代替更为简单:
$.ajax({
type: "POST",
url: "test.php",
data: datastring,
dataType: 'html',
success: function (data) {
$('#results').html(data);
}
});
Or 要么
$.ajax({
type: "POST",
url: "test.php",
data: datastring,
dataType: 'html'
}).done(function (data) {
$('#results').html(data);
});
See http://api.jquery.com/jQuery.ajax/ 参见http://api.jquery.com/jQuery.ajax/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.