client:
<script type="text/javascript" language="javascript"> function jquerys(sid,action){ var name = $("#name").val(); var cont = $("#con").val(); $.ajax({ url: "http://localhost/cs2.php", type: "POST", data: 'name='+name+'&cont='+cont+'', dataType:"text", cache:false, success: function(msg){ alert(msg); }, error:function (XMLHttpRequest, textStatus, errorThrown) { // alert(XMLHttpRequest.responseText); } }); } </script>
server:
echo '[{"demoData":"This Is The JSON Data"}]';
弹出:This Is The JSON Data
如果服务器端返回不是json数据,将不会弹出alert();
dataType参数设置:
"xml": 返回 XML 文档,可用 jQuery 处理。
"html": 返回纯文本 HTML 信息;包含 script 元素。
"script": 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了"cache"参数
"json": 返回 JSON 数据 。
"jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。
"text": 返回纯文本字符串
下面的也可以正确运行:
var code_gg = $("#check").val();
$.post("<?=base_url()?>/js/admin/chk_code.php?act=gg",{code:code_gg},function(msg){
if(msg!=1){
alert("验证码错误!");
$("#check").select();
return false;
}else{
document.forms[0].submit();
}
});
ajax与php传递汉字:
js编码:encodeURIComponent(shipping_name)
php接收:$getcom = iconv('UTF-8', 'GB2312//IGNORE', $_REQUEST["com"]);
字母和数字不需要编码