这是我的第一篇文章,如果有什么遗漏或者我表达得不够清楚,请见谅。所有的代码都在同一个php文件中。
我的ajax调用:
我的ajax调用:
$.ajax(
{
type: "POST",
url: window.location.href,
data: {func: 'genString'},
datatype: 'json'
})
.done(function ( response )
{
console.log( response );
console.log( repose.string );
});
这会落入页面上的if语句中。
if ( isset ($_POST['func'] && $_POST['func'] == 'genString')
{
exit(json_encode(myFunction()));
}
该函数在页面上运行。
function myFunction()
{
/* Would generate a string based on the database */
$arr = array('rows' => 1, 'string' => 'My test string');
// Changes values in the array depending on the database
return $arr;
}
当页面被加载时,运行此函数以生成数组,并使用字符串部分来显示它,使用行部分来设置浏览器中文本区域的高度。但是,当调用ajax时,console.log(response)
会记录{"rows":1,"string":"My test string"}
而不是一个对象。
然而,当我尝试记录或使用字符串时,console.log(response.string)
显示为未定义。
我以前做过这个,并且已经返回了一个对象,我可以在js中使用response.string
。我尝试使用JSON_FORCE_OBJECT,但结果没有影响。
repose.string
中有一个拼写错误。 - Dekel