在本文中,我们将带你了解将JSON从PHP返回到JavaScript?在这篇文章中,我们将为您详细介绍将JSON从PHP返回到JavaScript?的方方面面,并解答php返回json数据交给js常见
在本文中,我们将带你了解将JSON从PHP返回到JavaScript?在这篇文章中,我们将为您详细介绍将JSON从PHP返回到JavaScript?的方方面面,并解答php返回json数据交给js常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的java – 尝试使用JSON将数据从PHP返回到Android …、javascript – echo’d PHP编码通过AJAX调用的JSON返回到底是什么?、javascript – JSON将值返回到全局变量、javascript – 从php脚本返回JSON对象。
本文目录一览:- 将JSON从PHP返回到JavaScript?(php返回json数据交给js)
- java – 尝试使用JSON将数据从PHP返回到Android …
- javascript – echo’d PHP编码通过AJAX调用的JSON返回到底是什么?
- javascript – JSON将值返回到全局变量
- javascript – 从php脚本返回JSON对象
将JSON从PHP返回到JavaScript?(php返回json数据交给js)
我有一个通过jQuery AJAX调用的PHP脚本。我希望PHP脚本将JSON格式的数据返回给javascript。这是PHP脚本中的伪代码:
$json = "{";
foreach($result as $addr)
{
foreach($addr as $line)
{
$json .= $line . "\n";
}
$json .= "\n\n";
}
$json .= "}";
基本上,我需要将两个for循环的结果插入$ json中。
java – 尝试使用JSON将数据从PHP返回到Android …
尝试将带有JSON的PHP数据返回到Android.下面是我的PHP脚本
<?PHP
#
print(json_encode("[name=john]"));
#
?>
但是我在java中收到错误:ERROR / log_tag(907):解析数据时出错org.json.JSONException:JSONArray文本必须以'[‘在字符0处开头
解决方法:
json_encode需要一个实际的对象或数组来编码为json格式.此外,最好为响应标头设置内容类型.试试这个:
<?PHP
header('Content-type: application/json');
print json_encode(array('name' => 'john'));
?>
我不太了解java方面.正如nikc所述,json_encode将关联数组更改为json对象,将数值数组更改为json数组.
javascript – echo’d PHP编码通过AJAX调用的JSON返回到底是什么?
我想我在这里遗漏了一些东西:
使用AjAX我从数据库中获取一些数据并以JSON格式发回
$jsondata = array();
while ($Row = MysqL_fetch_array($params))
{
$jsondata[]= array('cat_id'=>$Row["cat_id"],'category'=>$Row["category"],'category_desc'=>$Row["category_desc"],'cat_bgd_col'=>$Row["cat_bgd_col"]);
};
echo("{\"Categories\": ".json_encode($jsondata)."};");
到目前为止我没有问题.
在cleint方面,我收到上面的内容
ajaxRequest.responseText
如果我这样做
var categoriesObject = ajaxRequest.responseText;
alert(categoriesObject);
我看到了我期望看到的内容,即警报中的整个数组.
一切都出错的是尝试访问响应.我得到的错误是“categoriesObject”不是一个对象 – 如果不是它是什么?我的错误是我甚至无法像这样访问它:
document.write(categoriesObject.Categories[0].category);
那么我做错了什么?
echo json_encode(array('Categories' => $jsondata));
要不就
echo json_encode($jsondata);
我没有看到添加类别的理由.
>您必须使用JSON.parse
(在大多数浏览器中提供,但也可用作script)在客户端解码JSON:
var data = JSON.parse(ajaxRequest.responseText);
>如果您想要非常正确,请添加
header('Content-type: application/json');
到您的PHP脚本.
javascript – JSON将值返回到全局变量
我的代码看起来像这样:
var thevariable = 0;
For(){
//somecode using thevariable
$.getJSON('',{},function(e){
//success and i want to set the returned value from PHP to my variable to use it in the forloop
thevariable = e.result;
});
}
我的主要问题是变量值保持为“0”,在整个For循环期间,虽然我只希望它在第一个循环中为“0”,然后它从PHP返回的结果将其用于for循环.
这里是我的真实代码,如果你需要看看:
var orderinvoice = 0;
for(var i=0; i<table.rows.length; i++){
var ordername = table.rows[i].cells[5].innerText;
var orderqty = ((table.rows[i].cells[1].innerText).replace(/\,/g,'')).replace(/Qty /g,'');
var orderprice = (table.rows[i].cells[2].innerText).replace(/\$/g,'');
var ordertype = table.rows[i].cells[3].innerText;
var orderlink = table.rows[i].cells[4].innerText;
$.getJSON('orderprocess.PHP', {'invoice': orderinvoice, 'pay_email': email, 'ord_name': ordername, 'ord_qty': orderqty, 'ord_price': orderprice, 'ord_type': ordertype, 'ord_link': orderlink}, function(e) {
console.log();
document.getElementById("result").innerText= document.getElementById("result").innerText + "Order #"+e.result+" Created Successfully ";
document.getElementById("invoker").innerText = ""+e.invoice;
orderinvoice = e.invoice;
if(i+1 == table.rows.length){
document.getElementById("result").innerText= document.getElementById("result").innerText + "With invoice #" + e.invoice;
}
});
解决方法:
在一个循环块中,在一个ajax完成之前将运行另一个并且这是javascript自然处理.对于您的情况,您可以在成功事件结束时调用函数.做这样的事情:
var i = 0;
doSt();
function doSt() {
var orderinvoice = 0;
var ordername = table.rows[i].cells[5].innerText;
var orderqty = ((table.rows[i].cells[1].innerText).replace(/\,/g, '')).replace(/Qty /g, '');
var orderprice = (table.rows[i].cells[2].innerText).replace(/\$/g, '');
var ordertype = table.rows[i].cells[3].innerText;
var orderlink = table.rows[i].cells[4].innerText;
$.getJSON('orderprocess.PHP', { 'invoice': orderinvoice, 'pay_email': email, 'ord_name': ordername, 'ord_qty': orderqty, 'ord_price': orderprice, 'ord_type': ordertype, 'ord_link': orderlink }, function(e) {
console.log();
document.getElementById("result").innerText = document.getElementById("result").innerText + "Order #" + e.result + " Created Successfully ";
document.getElementById("invoker").innerText = "" + e.invoice;
orderinvoice = e.invoice;
if (i + 1 == table.rows.length) {
document.getElementById("result").innerText = document.getElementById("result").innerText + "With invoice #" + e.invoice;
}
i++;
if (i < table.rows.length) doSt();
});
}
javascript – 从php脚本返回JSON对象
我正在使用jQuery向PHP文件发出AJAX GET请求.我希望PHP脚本返回一个JSON对象,但是,目前它正在返回一个JSON字符串.我意识到我可以在jQuery代码中使用JSON.parse,但是,我在向API返回一个JSON对象的AJAX调用时有任何经验.我试图用PHP脚本做同样的事情,但它返回一个字符串而不是一个对象.
有谁知道这里的最佳实践是什么,如果最佳实践是返回一个JSON对象我将如何使用PHP?
请参阅以下代码:
JS
$.get('test.PHP', function(data){
console.log((data));
});
PHP
<?PHP
$jsonAnswer = array('test' => 'true');
echo json_encode($jsonAnswer);
解决方法:
在PHP文件中,将内容类型更改为application / json.
JS
$.get('/process.PHP', function(data) {
console.log(data);
} );
PHP
<?PHP
header( "Content-type: application/json" );
$jsonAnswer = array('test' => 'true');
echo json_encode($jsonAnswer);
然后你的控制台应该读取Object {test:“true”},而不仅仅是JSON字符串.
我们今天的关于将JSON从PHP返回到JavaScript?和php返回json数据交给js的分享已经告一段落,感谢您的关注,如果您想了解更多关于java – 尝试使用JSON将数据从PHP返回到Android …、javascript – echo’d PHP编码通过AJAX调用的JSON返回到底是什么?、javascript – JSON将值返回到全局变量、javascript – 从php脚本返回JSON对象的相关信息,请在本站查询。
本文标签: