我有一个非常短的php代码,只是为了在Cordova应用程序中测试ajax。
php的代码是:
<?php
if (isset($_POST["TEST"])){
if ($_POST["TEST"] == "TEST"){
$resp["GOOD"] = "TEST WORKS!!";
echo (json_encode($resp));
}
}
?>我的请求代码是:
$.post('http://mobtest.bugs3.com/test.php',
{ TEST: 'TEST' },
function (result) {
console.log(result);
$('#txtlbl').text(result);
}
);我期望responseText是"TEST WORKS!!",但我得到的是:
{"GOOD":"TEST WORKS!!"}<!-- www.serversfree.com Analytics Code -->
<script src="http://www.serversfree.com"></script><noscript><a title="Free hosting servers" href="http://www.serversfree.com">Free servers</a><a title="Free websites hosting server" href="http://www.serversfree.com">Free websites hosting server</a><a title="Free hosting server features" href="http://www.serversfree.com/server-features/">Free server features</a><a title="Free hosting" href="http://www.bugs3.com">Free hosting</a><a title="Page rank" href="http://www.1pagerank.com">Page rank</a></noscript>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-24425628-3']);
_gaq.push(['_setDomainName', window.location.host]);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript" src="http://www.bugs3.com/ganalytics.js"></script>
<!-- End Of Analytics Code -->我不明白这是怎么回事。
发布于 2015-01-16 04:06:31
这里的答案可能会解决你的问题:https://stackoverflow.com/a/10768130/1696795
简而言之,用户cleong写道:
代码在
自动附加文件中。如果您显式退出,而不是让脚本到达文件末尾,则不会执行该脚本。
所以只需将这段代码附加到php代码的末尾:
exit();发布于 2015-01-16 04:06:20
由于某些原因,您试图对整个数组执行echo操作。
更新:加上您在尝试直接在javascript中访问json_encode时使用result。
所以只需在php代码中回显变量:
echo $resp["GOOD"];https://stackoverflow.com/questions/27971828
复制相似问题