Json response from http webserver not accepted by $getJSON

Hi all,
since the last years (when the panda II was releases) I’m playing around with GHI hardware and currently I’m doing some house automation based on this hardware. Now I’m playing around with G120 to migrate to. I already got a lot of information from this forum (thanks to everybody), but now it is time for my first own post :slight_smile:

Background:
Currently I’m building a remote web application to control the G120 software remotely. Therefore I basically use web server example from Gus (WIZnet W5100 HTTP FileServer) and jquery mobile and it works fine so far.

Problem:
For the dynamic data exchange I would like to use json and I use the following java script code:

var jqxhr = $.getJSON(“http:…”, function () {
})
.done(function () {
})
.fail(function () {
})
.always(function () {
});

This code works fine for public internet links, which provides json data, but if I change the link to my G120 IP address, only the fail method is entered.
On Server/G120 side I do not something complex, I only response some json (here from a web example):

            string responseString = "{\"status\":{\"type\":\"message\",\"value\":\"Valid JSON value found\"},\"original\":{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}}";
            byte[] message = encoder.GetBytes(responseString);
            response.ContentType = "application/json";
            response.OutputStream.Write(message, 0, message.Length);

If I use google chrome browser with json validator or any other online json validator, the G120 response is shown as valide.
But if I do the request from java script (see above), it fails anytime.

It would be great, if somebody could help me.

Thanks for all responses!

@ bin-blank - I would try these couple of things:

I am only stating this as we do not know how you are constructing the data on the JS side, but if you are constructing an array from your JSON data, that string would be incorrect.

There have been problems a couple of years ago – 2012 – with the JSON MIME type and certain clients failing to parse otherwise valid JSON data, this should have been fixed by now but you can try some of the suggestions listed here:

http://stackoverflow.com/questions/267546/correct-content-type-http-header-for-json

@ James - thanks for your replay!
I tried the following steps, but without success
:

  1. Simplifying the json to a string that should work definitely:
    string responseString = “{“Name”: “Foo”}”;
    In Chrome it’s accepted and shown as valid json.
    -> debug details: jqxhr: readyState = 0, responseText = “”, status = 0, statusText = “error”

  2. I tried both all types listed on stackoverflow (not at the same time )
    response.ContentType = “application/json”;
    response.ContentType = “text/json”;
    response.ContentType = “text/plain”;
    (this was my original link and the reason why I tried “application/Jason” :))
    -> same error in all three cases

  3. Although I trust Google Chrome, I copied the java script to JSFiddle and run the script and request there (in Chrome Version 32.0.1700.102 m and IE v10.0.).
    -> same error in both cases

  4. Because it is mentioned on stackoverflow, that maybe not the latest jquery version is used and I’m using:

I selected several versions on jsFiddle.
-> same error in both cases

Hopefully I caught all your suggestions and did not miss something.

From my point of view it seems on webserver side issue, but sere are only few lines of code. And I have no idea what should be changed.

All good ideas are welcome!
:wink:

@ bin-blank - with the MIME types, did you add the utf-8 charset tag? If it were to be an issue with MIME at this point, that would be the only other thing I could think of.

What module are you using for network communications?

@ James -I’m using ENC28 1.1 Ethernet Module.
After reading some websites, I tried to used same encoding as given by the request itself;
System.Text.Encoding encoding = response.ContentEncoding;
While debugging you can see, that it is utf-8, but still the same problem. I do not really believe that it is an encoding problem.
I assumed, that the ContentType was not transferred to the web request correctly and expected that the problem is on G120 side, but that does’nt seem to be the case.
I worte some lines of code in a windows console application:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(“http://dutack.no-ip.org”);
HttpWebResponse httpWebesponse = (HttpWebResponse)httpWebRequest.GetResponse();
Console.WriteLine(httpWebesponse.ContentType);

And the output is: ‘application/json’ as defined in the G120 code.

So I have to have a look at the other side, the java script, but it is already quite easy … maybe at the weekend, bit I’m not a java script prfessional.

I will continue playing around and will post a solution if I find some.
(Or I switch back to jsonp, that worked already with standard correct ContentType.)

Because the problem is not solved, I’m looking forward to all suggestions, ideas, links …
Thanks!

At any interested:
I think this is an interesting solution to control ports or all other hardware through a web interface. In my current solution I’m using HTML5 mechanisms to cache the website application on my smartphone including an automatic update mechanism. So, I’m able to switch e.g. lights or sprinkler on and off by using my phone or tablet. And because I use jquery mobile, the usability is great. If the problem is solved and I find time it could be an interesting sample for code share.

Problem is solved and it’s my fault: I requested json from the G120. Therefore, I started the webserver as described in my previous post and the response was valid, but:

To make it more comfortable to develop developing web pages, I use visual studio. If the java script web page starts from a different server/location, it is a cross domain call and that is not supported by for json. That can be done by using jsonp.

So, if the web page itself is located on the G120 too, it works fine.

For me it will not be a good solution, because testing & debugging take too much time if changes has to be transferred to the G120 first.