本文的目的是介绍php–使用$.ajax从json_encode接收响应时获取parseerror的详细情况,特别关注php接收ajax传的数据的相关信息。我们将通过专业的研究、有关数据的分析等多种方
本文的目的是介绍php – 使用$.ajax从json_encode接收响应时获取parseerror的详细情况,特别关注php接收ajax传的数据的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解php – 使用$.ajax从json_encode接收响应时获取parseerror的机会,同时也不会遗漏关于$.ajax从php获取json_encode($arr)之后的对象,但是如何在jQuery中获取键和值?、Ajax初级 json_encode、Javascript json_encode> JSON.parse()数组、jQuery,Ajax,JSON,PHP和parsererror的知识。
本文目录一览:- php – 使用$.ajax从json_encode接收响应时获取parseerror(php接收ajax传的数据)
- $.ajax从php获取json_encode($arr)之后的对象,但是如何在jQuery中获取键和值?
- Ajax初级 json_encode
- Javascript json_encode> JSON.parse()数组
- jQuery,Ajax,JSON,PHP和parsererror
php – 使用$.ajax从json_encode接收响应时获取parseerror(php接收ajax传的数据)
我正在尝试使用$.ajax via尝试一个非常简单的请求并获得响应. JSON.我已经尝试过从PHP文件中返回一个简单的文本短语.
我的js部分代码就是这样调用beemailer.PHP来获得响应.
$.ajax({
type: 'POST',
dataType: "json",
contentType: "application/json; charset=utf-8",
url: 'beemailer.PHP',
data: {
'name': data_name,
'email': data_email,
'message': data_message,
'subject': data_subject
},
success: function (data) {
var output = jQuery.parseJSON(data);
console.log(output);
alert(output);
},
error: function (error, x, y) {
console.log(error);
alert(x, y);
}
)
};
而beemailer.PHP是这样的:
<?PHP
$to = "ashish_sharma307@hotmail.com";
$subject = $_POST['subject'];
$message =$_POST['message'];
$headers = "From:".$_POST['name']."(".$_POST['email'].")";
mail($to,$subject,$message,$headers);
$response = "The mail has been sent. Thank You the valid mail.";
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($response);
?>
我现在需要的只是让$response文本得到警告.
任何帮助将不胜感激.
解决方法:
从ajax调用中删除以下代码并尝试:
contentType: "application/json; charset=utf-8",
不需要jQuery.parseJSON(data),因为Content-Type是通过以下代码在json中设置和编码的:
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($response);
更新了包含测试数据的代码
JS
$.ajax({
type: 'POST',
dataType: "json",
url : 'beemailer.PHP',
data: {name: 'rai', email: 'rai@test.com', message: 'Test message', subject: 'Test subject'},
success : function(data) {
console.log(data);
alert(data);
},
error: function(error, x, y)
{
console.log(error);
alert(x, y);
}
});
PHP(beemailer.PHP)
<?PHP
$to = "ashish_sharma307@hotmail.com";
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "From:" . $_POST['name'] . "(" . $_POST['email'] . ")";
mail($to, $subject, $message, $headers);
$response = "The mail has been sent. Thank You the valid mail.";
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($response);
?>
对象示例:
$result = array();
$result['status'] = 'success';
$result['message'] = 'The mail has been sent. Thank You the valid mail.';
header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($result);
JS访问对象:
$.ajax({
type: 'POST',
dataType: "json",
url : 'beemailer.PHP',
data: {name: 'rai', email: 'rai@test.com', message: 'Test message', subject: 'Test subject'},
success : function(data) {
alert('status: ' + data.status + '\nmessage: ' + data.message);
},
error: function(error, x, y)
{
console.log(error);
alert(x, y);
}
});
$.ajax从php获取json_encode($arr)之后的对象,但是如何在jQuery中获取键和值?
在PHP中:
$arr = array( 10=>"ten",5=>"five",2=>"two"); return json_encode($arr);
在JS – $.ajax()中:
success: function(data){ console.log(data);}
2
我在控制台中看到的是:
对象{2:“两个”,5:“五”,10:“十”},
我想用于(var i = 0; i< data.length,i)但是失败了. 最后它以这种方式工作:for(var i in data) 3 我的问题:为什么数组已排序?我希望数组保持未分类. 有人帮我吗?
解决方法
所以你得到一个对象而不是一个数组,并且没有标准说对象属性必须以任何特定方式排序或根本不排序.
您可以尝试将数据放在2个数组中
$arr = array( 'indecies'=>array(10,5,2),'values'=>array("ten","five","two") ); return json_encode($arr);
for(var i=0; i< data.indecies.length,i++){ // do something with //data.indecies[i] //data.values[i] }
Ajax初级 json_encode
Ajax:Asynchronous JavaScript and XML
Ajax用来异步通信,用户不需要长长的等待。
实现也很简单,主要用到XMLHttpRequest对象
HTML代码:
<body>
<form>
<p>City: <input type="text" name="city" id="city" size="25"
onChange="callServer();" /></p>
<p>State: <input type="text" name="state" id="state" size="25"
onChange="callServer();" /></p>
<p>Zip Code: <input type="text" name="zipCode" id="zipCode" size="5" /></p>
</form>
</body>
<script>
var
XMLHttp=
null
;//兼容多种浏览器
{
new
ActiveXObject(
"Msxml2.XMLHTTP"
);
}
catch
(e){
{
);
(XMLHttp==
){
var city=document.getElementById("city").value;
var state=document.getElementById("state").value;
if((city==null)||(city=="")) return;
if((state==null)||(state=="")) return;
var url="__APP__/Index/test2?city="+escape(city)+"&state="+escape(state);//这里用的thinkPHP框架,一般是xx/yy/z.PHP
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=updatePage;
xmlHttp.send(null);
if (xmlHttp.readyState==4) {
if (xmlHttp.status==200){
var response=xmlHttp.responseText;
document.getElementById("zipCode").value=response;
</script>
后台PHP:
public function test2(){
//获得来自 URL 的参数
$c=$_GET["city"];
$s=$_GET["state"];
//查找数组中的所有提示
if ((strlen($c) > 0)&&(strlen($s) > 0)){
$response = 112;
echo $response;
}
}
后台处理后的数据放到$response(这只是随便取的变量名,可以改的)里
然后前台的updatePage函数通过xmlHttp.responseText获取,就行啦~
如果要传数组,就要用json格式,比如:
$response=("1","2","3");
echo json_encode($response);
Javascript json_encode> JSON.parse()数组
我以下PHP代码:
echo "<img src='../images/edit.png' onclick='showEditDiv(" . json_encode($row) . ");'>";
这是HTML结果:
<img src='../images/edit.png' onclick='showEditDiv({"ID":"2","NAME":"John Smith","EMAIL":"johnsmith@domain.com"});'>
这是Javascript代码:
function showEditDiv(data) {
alert(data);
data = JSON.parse(data);
alert(data);
for (i=0; i< data.length; i++){
alert(data[i]);
};
}
问题是我没有在JS参数中获得所需的数组.
第一个警报显示“[object Object]”,这就是全部,不再有警报.
问题出在哪儿?我的代码基于我在这里找到的例子.我想要的是将一个数组传递给JS函数,该函数位于一个单独的.js文件中.我不想使用JQuery,更喜欢本机JS.
解决方法:
您没有将JSON传递给函数(JSON将是一个字符串,因此以引号结束/开头).您传递的是JavaScript对象文字.看到不同:
对象文字
{"ID":"2","NAME":"John Smith","EMAIL":"johnsmith@domain.com"}
JSON字符串
"{\"ID\":\"2\",\"NAME\":\"John Smith\",\"EMAIL\":\"johnsmith@domain.com\"}\"
// or, easier than escaping all those quotes..
'{"ID":"2","NAME":"John Smith","EMAIL":"johnsmith@domain.com"}'
当然,出于您的目的,对象文字可能更容易使用.在这种情况下,您只需要从JavaScript代码中删除JSON.parse:
function showEditDiv(data) {
// The below won't work unless you change your input data to a
// JS array literal..
/* for (i=0; i< data.length; i++){
alert(data[i]);
}; */
// ..but you can do this (though it's risky to iterate through keys
// on an object without at least using HasOwnProperty)
for (var key in data) {
alert(data[key]);
}
}
jQuery,Ajax,JSON,PHP和parsererror
jQuery片段:
$("form").submit(function(){ $.ajax({type: "POST",url: "inscrever_curso.PHP",data: {cpf : $("input#cpf").val(),nome : $("input#nome").val()},dataType: "json",success: function(data){ alert("sucesso: "+data.msg); },error: function(XMLHttpRequest,textStatus,errorThrown){ alert ("erro: "+textStatus); } }); return false; });
PHP:
<?PHP $return[msg]="Testing,testing."; echo json_encode($return); ?>
解决方法
$return[msg]="Testing,testing."; echo json_encode($return);
这样做,使用E_NOTICE error_reporting级别,您将获得:
Notice: Use of undefined constant msg - assumed 'msg'
如果在服务器上启用并显示通知,则会导致输出无效JSON(输出包含JSON字符串之前的错误消息).
要避免该通知,您必须使用以下语法:
$return['msg']="Testing,testing."; echo json_encode($return);
注意msg周围的引号:你创建的数组元素的键是一个字符串常量,因此,它用引号括起来.
如果你没有输入这些引号,PHP会搜索一个名为“msg”的defined常量,该常量可能不存在.
有关此内容的更多信息,您可以查看本手册的这一部分:Why is $foo[bar] wrong?.
(我不确定它会解决你的问题,但无论如何都要知道.)
关于php – 使用$.ajax从json_encode接收响应时获取parseerror和php接收ajax传的数据的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于$.ajax从php获取json_encode($arr)之后的对象,但是如何在jQuery中获取键和值?、Ajax初级 json_encode、Javascript json_encode> JSON.parse()数组、jQuery,Ajax,JSON,PHP和parsererror的相关信息,请在本站寻找。
本文标签: