views:

62

answers:

3

I have a url, the data of which page i need as a json object. I ve tried xmlhttprequest and ajaxobject both but doesnt work. It doesnt even give a responseText when I give it as an alert Ill post both the code snippets here. url = http://mydomain.com:port/a/b/c

AJAX :

var ajaxRequest = new ajaxObject(URL);

ajaxRequest.callback = function (responseText,responseStatus) {

alert(responseStatus);

JSONData = responseText.parseJSON();

processData(JSONData); }

USING xmlhttprequest:

var client = new XMLHttpRequest();

client.open('GET',URL,true );

data = JSON.parse(client.responseText);

alert(data.links.length);

can someone please help me out with this. I understand cross scripting may be an issue, but how to come over it? and shouldn't then too it should give the alerts as zero or null

A: 

I'm going to make a bit of an assuption here and say that you're trying to load data from a different domain using ajax?

If this is the case, you wont be able to do it using a standard ajax request. There are some services out there such as: http://www.ajax-cross-domain.com/ which aim to help overcome this when necessary.

Calling Cross Domain Web Services in AJAX also has some useful information on how you can configure the remote domain to accept incoming requests etc.

Hope this is of help.

Jamie Dixon
@jamie should it not atleast return zero/null or a statuscode of 0.. the problem is that its not showing anything. do you still think its to do with the cross domain scripting?
encryptor
A: 

I had a problem similar like this previously, where jquery had trouble reading the response because the server is returning content type text/html instead of application/json

If you don't set it, the default will be text/html.

If you use php, this is how you set it,

header('Content-type: application/json');

If you use jsp, this is how to set it,

response.setContentType('application/json');

This must be done before you are writing the response to client.

Install firebug and it lets you see the actual content-type being returned.

Make sure yours is application/json.

Rosdi
this worked! I also set the accept type as application/jsonwith both statements it worked for me!Thanks rosdi and jamie
encryptor
Good to hear that.., don't forget to mark my answer as accepted answer so that I can brag to my mom.. :-)
Rosdi
A: 

Consider using LitJSON.

LitJSON is a small and fast library for handling data in the JSON format. It is written in C# and is compatible with all .Net languages.

Lukas Šalkauskas