这更像是一个json问题,但因为它在wordpress中,并且正在由admin-ajax.php
我认为它最适合这个论坛。我创建了一个标准的ajax表单过程,如下所示。
HTML:
<form action="" method="post" id="stock-search-form" />
<input class="stock-search-element" type="search" name="stock-search-name" id="stock-search-id"/>
<input class="hide-me" type="hidden" name="action" value="process_stock" />
<button class="stock-button" name="stock-button-name" id="stock-button-id" type="send">Search</button>
</form>
<div class="stock-table-outer"></div>
PHP:
function process_stock() {
$the_php_result = array("foo1" => "String_1",
"foo2" => "String_2",
"foo3" => "String_3",
"foo4" => "String_4",
"foo5" => "String_5",
"foo6" => "String_6",
"foo7" => "String_7",
"foo8" => "String_8",
"foo9" => "String_9",
"foo10" => "String_10",
"foo11" => "String_11",
"foo12" => "String_12",
"foo13" => "String_13",
);
echo json_encode($the_php_result);
}
JQUERY:
jQuery(\'#stock-search-form\').submit(ajaxSubmit);
function ajaxSubmit(){
var StockSymbolUI = jQuery(this).serialize();
jQuery.ajax({
type:"POST",
url: "/wp-admin/admin-ajax.php",
data: StockSymbolUI,
success:function(data){
jQuery(".stock-table-outer").show();
jQuery(".stock-table-outer").html(data);
}
});
return false;
}
我在前端得到的结果是:
{"foo1":"String_1","foo2":"String_2","foo3":"String_3",
"foo4":"String_4","foo5":"String_5","foo6":"String_6",
"foo7":"String_7","foo8":"String_8","foo9":"String_9",
"foo10":"String_10","foo11":"String_11","foo12":"String_12",
"foo13":"String_13"}
乍一看,这看起来像一个json对象,但我不相信它是。
我想做的是访问对象的元素,但是如果我data[0]
而不是data
在jquery代码的成功中,我得到的只是{
, i、 e.字符串的第一个字符。我试过使用JSON.parse(data)
或jQuery.parseJSON(data)
正如其他地方所建议的那样,但我没有得到任何回报。
任何帮助都将不胜感激,因为我已经为此坚持了一整天。。。
骚扰