0

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.

Dawson Loudon
  • 6,029
  • 2
  • 27
  • 31
Priyabrata
  • 1,202
  • 3
  • 19
  • 57
  • 1
    Your host is injecting an advertisement into your response?? – Sterling Archer Jan 15 '15 at 19:52
  • 4
    This is why you should pay for hosting (its only like $4 a month) rather than using free hosting. Free hosting appends stuff to your response. – developerwjk Jan 15 '15 at 19:53
  • 1
    I suggest DigitalOcean.com. $5 a month, amazing service. Full root control of your server – Sterling Archer Jan 15 '15 at 19:54
  • Thing is, this is not happening when I am using GET and running it in chrome. There everything is perfect. When I am sending a post from a cordova app, I am getting this. More-over I've used this server in my applications for testing purpose and never saw anything like this. – Priyabrata Jan 15 '15 at 20:01

2 Answers2

2

The answer here might solve your problem: https://stackoverflow.com/a/10768130/1696795

In short user cleong wrote:

The code is in a PHP auto-append file. It doesn't get executed if you exit explicitly instead of letting the script reach the end of the file.

so just append this to the end of your php code:

exit();
Community
  • 1
  • 1
Mario A
  • 3,286
  • 1
  • 17
  • 22
  • I did this, but it didn't help. When I used a paid server, the problem disappeared with my existing code with no changes made to it. – Priyabrata Jan 16 '15 at 05:44
1

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"];
low_rents
  • 4,481
  • 3
  • 27
  • 55
  • 1
    Why `json_encode` the text response instead of the array? The purpose of the function is to access it as an array within javascript. – Jon Jan 15 '15 at 20:10
  • @Jon yes, sure. but he doesn't - he is just using the response directly. plus, i have to update my answer. he should just echo the value without using `json_encode` at all. – low_rents Jan 15 '15 at 20:13
  • This is true, but I was assuming it was just for testing purposes. Though, it still won't remove the addon code that the host adds. ^^ – Jon Jan 15 '15 at 20:15