好的,所以我正在尝试从ajax请求中检索数据,并且可能的话设置一个PHP数组…我什至不确定这是否可能考虑一个是客户端而一个是服务器端…所以这是我的代码
在html页面上
<?PHP foreach ($products as $product): ?>
<li>
<h3><span><?PHP print $product["price"]; ?></span> (4) <?PHP print $product["name"]; ?> </h3>
<div class="container">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="42"><span class="qty"><?PHP print $product["quanitity"]; ?></span></td>
<td width="180">
........
我想遍历该数组中的所有产品,但是要获得这些产品,我需要像这样进行ajax调用
$.ajax({
type: 'post',
url: "/shop_systems/index.PHP?route=module/cart/get_all_products",
dataType: 'json',
data: {current : null, prevIoUs : these_ids, quantity : 1, multiple : true},
success: function (data) {
我在想是否有一种简单的方法可以做到这一点….我在想一个解决方案是在ajax调用的成功部分中编写html,但是我会有很多的append语句…任何更干净的方法将不胜感激
解决方法:
在PHP中使用cURL
PS:我建议您不要像以前那样混合使用html和PHP.不必要地很难调试和维护.
您可以使用以下代码(未经测试,但应该可以使用):
注意:将网址设为绝对网址,否则将无法正常工作
$post_data = array(
'current' => 'null',
'prevIoUs' => 'null',
'quantity' => '1',
'multiple' => 'true'
);
// Init curl thing
$ch = curl_init();
// Setup curl options
curl_setopt_array($ch, array(
CURLOPT_URL => '/shop_systems/index.PHP?route=module/cart/get_all_products',
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE, // Return content as string
CURLOPT_FOLLOWLOCATION => TRUE, // Follow redirects
CURLOPT_AUTOREFERER => TRUE, // Set referer field on redirect
CURLOPT_VERBOSE => TRUE,
CURLOPT_COOKIEJAR => '/tmp/curl_PHP_cookies',
CURLOPT_COOKIEFILE => '/tmp/curl_PHP_cookies',
CURLOPT_STDERR => fopen('/tmp/curl_PHP_log', 'w'),
CURLOPT_POSTFIELDS => $post_data
));
// Excecute request
$product = json_decode(curl_exec($ch));
$html = '';
foreach ($products as $product)
{
$html .= <<< HTML
<li>
<h3>
<span>{$product["price"]}</span>
(4) {$product["name"]}
</h3>
<div class="container">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="42"><span class="qty">{$product["quanitity"]}</span></td>
<td width="180">
HTML;
}
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。