douxuqiao6394 2015-01-15 19:50
浏览 61

获得奇怪的responseText

I've a very short php code written just for testing the ajax in a Cordova application.

Code for php is :

<?php

    if (isset($_POST["TEST"])){
        if ($_POST["TEST"] == "TEST"){
            $resp["GOOD"] = "TEST WORKS!!";
            echo (json_encode($resp));
        }
    }
?>

My request code is :

    $.post('http://mobtest.bugs3.com/test.php',
        { TEST: 'TEST' },
        function (result) {
            console.log(result);
            $('#txtlbl').text(result);
        }
    );

I am expecting the responseText to be "TEST WORKS!!" but what I get is :

{"GOOD":"TEST WORKS!!"}&lt;!-- www.serversfree.com Analytics Code --&gt;
&lt;script src="http://www.serversfree.com"&gt;&lt;/script&gt;&lt;noscript&gt;&lt;a title="Free hosting servers" href="http://www.serversfree.com"&gt;Free servers&lt;/a&gt;&lt;a title="Free websites hosting server" href="http://www.serversfree.com"&gt;Free websites hosting server&lt;/a&gt;&lt;a title="Free hosting server features" href="http://www.serversfree.com/server-features/"&gt;Free server features&lt;/a&gt;&lt;a title="Free hosting" href="http://www.bugs3.com"&gt;Free hosting&lt;/a&gt;&lt;a title="Page rank" href="http://www.1pagerank.com"&gt;Page rank&lt;/a&gt;&lt;/noscript&gt;
&lt;script type="text/javascript"&gt;

  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);
  })();

&lt;/script&gt;
&lt;script type="text/javascript" src="http://www.bugs3.com/ganalytics.js"&gt;&lt;/script&gt;
&lt;!-- End Of Analytics Code --&gt;

I don't understand whats happening here.

  • 写回答

2条回答 默认 最新

  • dongzhan1570 2015-01-15 20:06
    关注

    for some reason, you are trying to echo your whole array.

    update: plus you use json_encode while trying to access the result directly in your javascript.

    so just echo the variable in your php code:

    echo $resp["GOOD"];
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值