-1

I have a french website http://techtionary.comeze.com/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.com Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/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

corroded
21.7k19 gold badges88 silver badges133 bronze badges
asked May 26, 2011 at 3:46
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. 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 Commented May 26, 2011 at 3:55
  • but in my json is delivered from php Commented May 26, 2011 at 3:58

6 Answers 6

3

Using header('Content-Type: application/json'); is not enough for 000webhost.

Calling die; at the end of the script removes unwanted html from AJAX requests.

answered May 2, 2012 at 14:33
Sign up to request clarification or add additional context in comments.

Comments

2

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 :)

answered May 26, 2011 at 4:01

1 Comment

i just checked your page and in your case you will substring from the begining of the added text till the end of the document
2

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.com/load/ and read the section titled "Loading Page Fragments."

HTH.

answered May 26, 2011 at 4:33

Comments

2

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);
})
answered May 26, 2011 at 3:55

Comments

2

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 coming 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.

answered May 26, 2011 at 14:16

Comments

0

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.

answered May 26, 2011 at 3:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.