0

I wrote some php code that outputs some valid json, and sets the content-type header to application/json in my dev setup. However when I deploy this script to a embedded webserver it works fine except it's not capable of sending the content-type. It's not possible to run a other webserver.

Now I have the following code for Dynatable. Even though my dev and my embedded webserver, serve exactly the same file, and the only difference is the content-type. It works for my dev setup, however it doesn't work for my embedded setup.

I use the following code to load the json file to dynatable.

document.ready(
    $.ajax({
        url: 'phpApi.php',
        success: function(data){
            $('#myTable').dynatable({
                dataset: {
                    records: data
                }
            });
        }
    }));

So can someone explain me why the content-type is so important for ajax? How can I tell my code manually its json?

Dr. Banana
  • 435
  • 1
  • 7
  • 16

2 Answers2

1

Without the content-type the returned data is assumed to be plain text. There is nothing in your code to tell it otherwise.

One way to get json would be to specify the return type in the jquery code. Just add dataType: 'json' into the ajax configuration.

Or you could use eval() to transform the returned text to json.

document.ready(
    $.ajax({
        url: 'phpApi.php',
        success: function(data){
            $('#myTable').dynatable({
                dataset: {
                    records: eval(data)
                }
            });
        }
    }));

Using JSON.stringify(eval(data)) might give you better results by making sure its json.

As pointed out below, JSON.parse(data) would probably be safer. (Eval is evil after all.)

Coin_op
  • 10,568
  • 4
  • 35
  • 46
  • 2
    Use `JSON.parse`, not `eval`. `eval` will run any arbitrary JavaScript, whereas `JSON.parse` will just convert JSON. – Jacob Jul 08 '15 at 23:17
  • I already tried dataType: 'json', however that did not solve the problem. So i'll try your other suggestion. – Dr. Banana Jul 08 '15 at 23:20
  • @Jacob I tried both JSON.parse and eval, where JSON.parse returned 0, and eval did the job. I do know it would be a security risk, this webserver is running on a local network not connected to the internet, so I think it will be fine. – Dr. Banana Jul 08 '15 at 23:25
  • Sounds like the content isn't really JSON then. – Jacob Jul 08 '15 at 23:30
  • That looks like valid JSON indeed. Not sure why `JSON.parse` wouldn't work if you're getting that string. Are you sure you're not getting back an _object_ and trying to parse what's already been parsed? – Jacob Jul 08 '15 at 23:56
0

So can someone explain me why the content-type is so important for ajax?

It's important so the client can identify what type of content the server returned, content-type: application/json tells jQUery to parse the data as an object. If no content type is returned, the client will assume the returned data is just plain text.

How can I tell my code manually its json?

Add dataType: "json" parameter to $.ajax()

document.ready(
    $.ajax({
        url: 'phpApi.php',
        dataType: "json",
        success: function(data){
            $('#myTable').dynatable({
                dataset: {
                    records: data
                }
            });
        }
    }));
angelcool.net
  • 2,505
  • 1
  • 24
  • 26