I have a french website .php
This is the link
the signup works(p.s if you enter any user name and password it will let you in)
When I do an ajax request my god damn free hosting adds this string of text to every page.
<!-- www.000webhost Analytics Code -->
<script type="text/javascript" src=".php"></script>
<noscript><a href="/"><img src=".php" alt="web hosting" /></a></noscript>
<!-- End Of Analytics Code -->
This causes my json and html to be unreadable
How do I cut off this string. Also any other suggestion why website is not working.
P.s I asked a question about my website earlier but it was kinda different
I have a french website http://techtionary.eze./game.php
This is the link
the signup works(p.s if you enter any user name and password it will let you in)
When I do an ajax request my god damn free hosting adds this string of text to every page.
<!-- www.000webhost. Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24./count.php"></script>
<noscript><a href="http://www.hosting24./"><img src="http://analytics.hosting24./count.php" alt="web hosting" /></a></noscript>
<!-- End Of Analytics Code -->
This causes my json and html to be unreadable
How do I cut off this string. Also any other suggestion why website is not working.
P.s I asked a question about my website earlier but it was kinda different
Share Improve this question edited May 26, 2011 at 3:47 corroded 21.6k19 gold badges86 silver badges133 bronze badges asked May 26, 2011 at 3:46 TomTom 5433 gold badges7 silver badges21 bronze badges 3-
2
Are you setting content type headers? I would assume your hosting provider only adds that code to content that is served with the text/html MIME type, which also happens to be the default. If you try setting a content type header to
application/json
you might find it works correctly. – Endophage Commented May 26, 2011 at 3:50 - you can also try and use mod_rewrite so instead of calling a .php script when you do the ajax request, you would call a .json file – Claudiu Commented May 26, 2011 at 3:55
- but in my json is delivered from php – Tom Commented May 26, 2011 at 3:58
6 Answers
Reset to default 3Using header('Content-Type: application/json');
is not enough for 000webhost.
Calling die;
at the end of the script removes unwanted html from AJAX requests.
i had the same issue recently as i was testing a small website for a client here is what i did:
since the ad is consistent and always the same, i return the data as text:
req.responseText;
i look for the the end of the add message : ended in </iframe>
in my case and replace it with a blank string;
var outMsg = xhr.responseText;
var index = (outMsg.indexOf("</iframe>") == -1 )? outMsg.length : outMsg.indexOf("</iframe>"); // my case ended with iframe, yours <!-- End Of Analytics Code -->
outMsg = outMsg.substring(0,index);
outMsg = outMsg.replace(/^\s*/, "").replace(/\s*$/, ""); // clear leading white space if any
now you can eval your ajax or convert it to any data type you want :)
Since you're already using .load()
, you could try adding another 'selector' to the method to only return a fragment of the page. Go here http://api.jquery./load/ and read the section titled "Loading Page Fragments."
HTH.
You can use the dataFilter option in jquery.ajax to filter the response text. Use this function to process the response text and strip down the add content and return the processed value.
You can try with this instead of load()
var regex = new RegExp("\<h2\>.+\</h2\>");
jQuery.ajax({
url : "score.php",
type: "GET",
dataType: "html",
dataFilter : function(responseText, dataType){
return regex.exec(responseText)[0];
}
}).success(function(result){
$("#points").html(result);
});
You can use the following script to debug
var regex = new RegExp("\<h2\>.+\</h2\>");
jQuery.ajax({
url : "score.php",
type: "GET",
dataType: "html",
dataFilter : function(responseText, dataType){
a = responseText;
console.log("filter");
console.log(responseText);
return regex.exec(responseText)[0];
}
}).success(function(){
console.log("success");
console.log(arguments);
}).error(function(){
console.log("failure");
console.log(arguments);
})
So to answer your question on how to set the content type header, it's nice and easy. Headers have to be output before any real content, so anywhere before you start echoing/printing your json output just add the line:
header('Content-Type: application/json');
This tells both the server and the browser that the content that's ing up has the json type and unless the sysadmin for your hosting provider is a total moron, that shouldn't get tagged with that analytics code.
You can use firebug in Firefox or Web Inspector in Safari/Chrome to make sure the header is being set correctly.
Well, the 'free host' doesn't seem to mind making your page's html invalid - that suggests you should not expect your pages to work reliably on this host. Try a non-free host - they work better.