JSON 从 PHP 到 javascript
JSON from PHP to javascript
我在 json 中收到了一个数组。如何使用 bucle for in javascript 获取值?
如果数组 json 没有分配变量名
我在 php 中有一个数组,它转换为 json
echo json_encode($error);
我得到以下 json:
json
中的值
["El nombre del usuario no puede estar vacio","La contrase\u00f1a debe tener un m\u00ednimo de
7 caracteres"]
如何在 javascript 我的函数 ajax
中获取数据
function submitForm()
{
var dataString = $("#userForm").serialize();
console.log(dataString);
$.ajax({
type: "POST",
url: "/altausers",
data: dataString,
success: function(text)
{
if(text==='success')
{
}
else
{
$("#error").removeClass('hidden');
#get values json here
}
}
});
}
PHP 有称为 json_encode 和 json_decode 的方法,我可以用它来处理 php 中的 json 数据,而 json 数据可以在 javascript 中直接访问,所以我认为你应该研究一下
您必须指定
dataType: 'json'
那么,您的数据将在 javascript 数组中:
function submitForm()
{
var dataString = $("#userForm").serialize();
console.log(dataString);
$.ajax({
type: "POST",
url: "/altausers",
data: dataString,
dataType: 'json',
success: function(text)
{
if(text.length > 0) {
$.each(text,function(index,value) {
alert('Response: '+ value);
}
}
});
}
但是,您必须确保 php 总是 returns 一个 json 编码的响应...即使您像这样手动执行:
<?php echo "[success]"; ?>
在 javascript 中,您只需测试
if(text[0] == 'success') { //hurray!!
}
我在 json 中收到了一个数组。如何使用 bucle for in javascript 获取值? 如果数组 json 没有分配变量名
我在 php 中有一个数组,它转换为 json
echo json_encode($error);
我得到以下 json:
json
中的值["El nombre del usuario no puede estar vacio","La contrase\u00f1a debe tener un m\u00ednimo de
7 caracteres"]
如何在 javascript 我的函数 ajax
中获取数据 function submitForm()
{
var dataString = $("#userForm").serialize();
console.log(dataString);
$.ajax({
type: "POST",
url: "/altausers",
data: dataString,
success: function(text)
{
if(text==='success')
{
}
else
{
$("#error").removeClass('hidden');
#get values json here
}
}
});
}
PHP 有称为 json_encode 和 json_decode 的方法,我可以用它来处理 php 中的 json 数据,而 json 数据可以在 javascript 中直接访问,所以我认为你应该研究一下
您必须指定
dataType: 'json'
那么,您的数据将在 javascript 数组中:
function submitForm()
{
var dataString = $("#userForm").serialize();
console.log(dataString);
$.ajax({
type: "POST",
url: "/altausers",
data: dataString,
dataType: 'json',
success: function(text)
{
if(text.length > 0) {
$.each(text,function(index,value) {
alert('Response: '+ value);
}
}
});
}
但是,您必须确保 php 总是 returns 一个 json 编码的响应...即使您像这样手动执行:
<?php echo "[success]"; ?>
在 javascript 中,您只需测试
if(text[0] == 'success') { //hurray!!
}