要发送的 ajax 请求的 html 格式不起作用
Html format to send for ajax request not working
我正在尝试在将html格式发送到ajax请求之前获取html格式。但是发生了这样的错误:Error: SyntaxError: Unexpected token < - parsererror - [object Object]
我只想通过数组中的值进行选择选项。这是代码:
$args = array(
'type' => 'ad_listing',
'child_of' => $parent_cat,
'orderby' => 'name',
'order' => 'ASC',
// 'hide_empty' => 1,
// 'hierarchical'=> 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'ad_cat',
'pad_counts' => false
);
$results = get_categories( $args );
$result = '<select>';
foreach ($results as $key => $value) {
$result .= '<option>' . $value . '</option>';
}
return $result .= '</select>';
// return the result to the ajax post
die( json_encode( array( 'success' => true, 'html' => $result ) ) );
在这里$result
将是我要发送的 html 格式。错误来了:Error: SyntaxError: Unexpected token < - parsererror - [object Object]
但是如果我以这种方式使用代码,我会得到数组对象:
$args = array(
'type' => 'ad_listing',
'child_of' => $parent_cat,
'orderby' => 'name',
'order' => 'ASC',
// 'hide_empty' => 1,
// 'hierarchical'=> 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'ad_cat',
'pad_counts' => false
);
$result = get_categories( $args );
// return the result to the ajax post
die( json_encode( array( 'success' => true, 'html' => $result ) ) );
我确定我做错了什么:(
正如我假设这是一个 WordPress 问题get_categories返回一个对象数组 (https://codex.wordpress.org/Function_Reference/get_categories)。
执行以下操作:
foreach($results as $cat) {
$result .= '<option>'.$cat->name.'</option>';
}
。或者根据WordPress Codex文档需要的任何内容。
相关文章:
- PHP:显示sqlite数据库中的html格式数据,使用tinymce保存
- JavaScript生成html格式的密码
- 如何在angularjs中实现html格式的字符串
- 如何重写Javascript If语句以选择Classes而不是ID's的HTML格式
- 如何使用JQUERY解析大型XML文件并将其可视化为HTML格式
- 正则表达式,用于从html格式的字符串中提取文本
- 表单中的CSS、HTML格式
- 以 HTML 格式预加载图像
- 如何使用 Ajax 以 html 格式呈现没有布局的 Rails 操作
- 使用JSONPjquery获取HTML格式的XML数据
- 如何在angular js中从github获取原始自述文件后将其转换为html格式的文档
- 从字面上看,以HTML格式输出PHP代码
- 循环多维数组并以 html 格式显示数据
- 从单选按钮中获取值以选择 html 格式的图像
- 以 HTML 格式显示计算结果 (javascript)
- 在 SciTE 中测试和编辑 JavaScript(独立和 HTML 格式)
- 从另一个网页以 HTML 格式通过电子邮件发送当前购物车
- HTML 保留字符串与 html 格式
- 导入 html 格式的外部 js 文件
- 如何使用多子对象值构建 angularjs html 格式