1

Parsing what's dished out (I'm looking at you Godaddy)

Posted October 23rd, 2009 in JQuery, Web Development and tagged , , , , , , , , , by Jon

It’s no secret – well if you’ve been listening to me for the past few days, that I’ve been upset with GoDaddy’s free web hosting. I have a side project for a client, and it needed an admin back end done – something to edit the pages, nothing crazy or too difficult and since I love JQuery, I add in some eye candy because it makes it easier to use.

Well as I’m cruising along, certain things that I’m use to doing a certain way just aren’t working. I narrowed down what I thought the problem was and it points right to the stupid ad banner that GoDaddy uses – well more to the point the mechanism they employ to add it to the pages.

It goes as far as attaching to an AJAX request reponse. So when you send your request and you get back a nice little JSON object back, it has some iframe crap stuck to the end of it. It really through me for a loop for awhile. Luckily Scott (and old coworker and friend of mine) got me pointed in the right direction.

I tend to do things the same way every time because it’s easy and it works, why make things difficult when I’m trying to get a project done. Well I spent some time trying to figure out how to get around this problem, and I didn’t like it – I looked for an answer everywhere. But Scott suggested that I take the result object and remove the offending string – granted I had thought of that but the method I was employing wasn’t having it – I couldn’t get anything to run inside the call back function.

$.ajax("ajax_get_page.php",{id:id},function(data) {
	console.log(data);
},"json");

So even this was failing, I’d get no log in FireBug, just the response which showed that crap on the end. So I started digging around on the tip from Scott, to look at just returning the object – I know it’s not making sense the way I’m explaining it, but follow me here, the code will explain it.

$.ajax({
	  url: "ajax_get_page.php",
	  type: "get",
	  data: ({id : id}),
	  cache: false,
	  success: function(html){
		  good = html.split("</iframe>");
		  myData = JSON.parse(good[0], function (key, value) {
			var type;
			if (value && typeof value === 'object') {
				type = value.type;
				if (typeof type === 'string' && typeof window[type] === 'function') {
					return new (window[type])(value);
				}
			}
			return value;
		});
		$("#wide").html(myData.wide_col);
	  }
	},"json");

Alright, follow with me…
url: the php will return what I want, echoed through json_encode – basically 2 pieces;
type: either post or get, i’m using get
data: need to pass it the id to get the right record
cache: true or false – will add a number to the end of the get to keep it from getting cached
success: if this succeeds do something

Now I’m going to break off the crap string I don’t want. So I look for the offending </iframe> piece and split it there. The next bit is something I found over at json.org, that helps parse the resulting object. Then I’m left with something more useable, and more familar the myData object. So I can set the html of #wide to the value of myData.wide_col.

Man that worked out well, and the ad banner is still intact so no legal issues arise.

Updated: The more I think about it, the safest way to do this would be to find the last occurrence of </iframe> just incase they decide to use an iframe in the page. I doubt this will come up in the project I’m working for, but for anyone else who might employ this method – be aware.

One Response so far.

  1. Ryan says:

    Thanks for this post.. I was banging my head on the wall trying to figure out why I couldnt get my ajax calls to work on free hosting via godaddy. Altho I couldnt get your code to work, I did fork over the money to get hosting to test out if that was my problem, and it was. You saved me a lot of time. Thanks

Leave a Reply





CommentLuv badge