Javascript jQuery 从 php json_encode 获取数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14281647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery getting array from php json_encode
提问by the_summer_bee
I created an array in php. I simply want to get the data it in jquery
我在 php 中创建了一个数组。我只是想在 jquery 中获取数据
here is the array in support.php:
这是 support.php 中的数组:
$postData = array(
"error" => $error,
"successInfo" => $successInfo,
"email" => $_POST["email"],
"subject" => $_POST["subject"],
"description" => $_POST["description"],
);
echo json_encode($postData);
What shall I do in javascript side using jquery.getJSON?
我应该在 javascript 端使用 jquery.getJSON 做什么?
Thanks in advance!
提前致谢!
采纳答案by Mohammad Adil
You can access an array in this way
您可以通过这种方式访问数组
$.ajax({
type: 'POST',
url: 'support.php',
success: function(result) {
$('#content1').html(result[0]);
},
});
回答by Expedito
It depends a lot on what you want todo with it, but this is a basic way of accessing the element keys. You can simply use the dot operator for each element key: "data.email" etc.
这在很大程度上取决于您想用它做什么,但这是访问元素键的基本方法。您可以简单地为每个元素键使用点运算符:“data.email”等。
$.ajax({
type: 'POST',
url: 'support.php',
success: function(result) {
var data = jQuery.parseJSON(result);
alert(data.email);
}
});
INSERT INTO HTML ELEMENT:
插入 HTML 元素:
I created a div with id="landingPad" and swapped out the alert line with:
我创建了一个 id="landingPad" 的 div 并用以下内容换掉了警报行:
$('#landingPad').html(data.email);
MAKE A LIST OF THE DATA RECEIVED:
列出收到的数据:
Then I changed my div into an unordered list:
然后我把我的 div 改成了一个无序列表:
<ul id="landingPad"></ul>
After changing the success function, it listed all the data received from support.php:
更改成功函数后,列出了从support.php接收到的所有数据:
$(document).ready(function(){
$.ajax({
type: 'POST',
url: 'support.php',
success: function(result) {
var data = jQuery.parseJSON(result);
$.each(data, function(index, value) {
$("#landingPad").append("<li>" + value + "</li>");
});
}
});
});
CREATE FORM ELEMENTS WITH AJAX DATA:
使用 AJAX 数据创建表单元素:
Next, I created the following form:
接下来,我创建了以下表单:
<form name="http://example.com/edit_my_values" action="post">
<div id="landingPad"></div>
<input type="submit" name="go" value="Edit Values"/>
</form>
Then by editing the AJAX, I created a form on the fly with the values received:
然后通过编辑 AJAX,我使用收到的值动态创建了一个表单:
$(document).ready(function(){
$.ajax({
type: 'POST',
url: 'support.php',
success: function(result) {
var data = jQuery.parseJSON(result);
$.each(data, function(index, value) {
$("#landingPad").append('<input type="input" name="'+index+'" value="'+value+'"/><br/>');
});
}
});
});
INSERT DATA INTO AN EXISTING FORM:
将数据插入现有表格:
Given the following form:
给定以下形式:
<form name="http://example.com/edit_my_values" action="post">
<label for="error">Error </label><input type="text" name="error"/><br/>
<label for="successInfo">Success </label><input type="text" name="successInfo"/><br/>
<label for="email">Email </label><input type="text" name="email"/><br/>
<label for="subject">Subject </label><input type="text" name="subject"/><br/>
<label for="description">Description </label><input type="text" name="description"/><br/>
</form>
You can fill your fields with AJAX data as follows:
您可以使用 AJAX 数据填充字段,如下所示:
$(document).ready(function(){
$.ajax({
type: 'POST',
url: 'support.php',
success: function(result) {
var data = jQuery.parseJSON(result);
$.each(data, function(index, value) {
$('[name='+index+']').val(value);
});
}
});
});
回答by Pran
jQuery
jQuery
success: function(result) {
var obj = jQuery.parseJSON(result);
alert(obj.link);
}
Note: Use jQuery.parseJSON()function, to get all data without getting an error.
注意:使用jQuery.parseJSON()函数,获取所有数据而不会出错。
php
php
Tip: If there are any possibility that array may contain html tagsthen encode data like that:
提示:如果数组可能包含html 标签,则对数据进行如下编码:
$data['success']='1';
$data['link']="<a href='http://stackoverflow.com/' target='_blank' style='color:#68dff0'>Click Here</a>";
echo json_encode($data, JSON_HEX_QUOT | JSON_HEX_TAG);
回答by jim dif
Here is a full working version:
这是一个完整的工作版本:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
$("#submit").click(function(){
fn_val=$('#fn').val();
ln_val=$('#ln').val();
$.ajax({
type: 'POST',
url: 'resp.php',
data : {fn: fn_val , ln: ln_val},
success: function(result) {
var data = $.parseJSON(result);
$("#the_results").append('Associative array returned' + '<BR>');
$.each(data, function(index, value) {
$("#the_results").append("index = " + index + ', data = ' + value + '<BR>');
});
}
});
});
});
</script>
</head>
<body>
<div>
First name: <input type="text" name="fn" id='fn'>
Last Name: <input type="text" name="ln" id='ln'>
<button id="submit">Pass Data to PHP</button>
</div>
<div id="the_results"></div>
</body>
</html>
Thats the html file, below is the php file resp.php
这就是html文件,下面是php文件resp.php
<?php
$resp_arr = array('First Name' => $_POST["fn"], 'Last Name' => $_POST["ln"]);
echo json_encode($resp_arr);
?>

