Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the JSFiddle echo feature with JQuery?

Tags:

I have read the jsFiddle user's guide for their JSON echo feature but with no luck of producing a working jsFiddle to echo a JSON message using JQuery.

How can I create a jsFiddle to echo the JSON from their guide:

data: {
    json: JSON.encode({
        text: 'some text',
        array: [1, 2, 'three'],
        object: {
            par1: 'another text',
            par2: [3, 2, 'one'],
            par3: {}
        }
    }),
    delay: 3
}

The one example provided is in Mootools which I've never used. So a simple translation from the mootools example into JQuery would be sufficient.

like image 453
kasdega Avatar asked Aug 25 '11 17:08

kasdega


2 Answers

var data = {
        json: $.toJSON({
            text: 'some text',
            array: [1, 2, 'three'],
            object: {
                par1: 'another text',
                par2: [3, 2, 'one'],
                par3: {}
            }
        }),
        delay: 3
}


$.ajax({
    url:"/echo/json/",
    data:data,
    type:"POST",
    success:function(response)
    {
       console.log(response);
    }
});

Live Demo

Note I have added na additonal resource.. jquery-json

Demo with FireBug Console on View (no need to pull up developer console to see return)

like image 92
Baz1nga Avatar answered Sep 24 '22 21:09

Baz1nga


You can also use JSON.stringify

$.ajax({
  url:"/echo/json/",
  data:{
    json: JSON.stringify({
        text: 'some text',
        array: [1, 2, 'three'],
        object: {
            par1: 'another text',
            par2: [3, 2, 'one'],
            par3: {}
        }
      }),
    delay: 3
  },
  type:"POST",
  success:function(response) {
    console.log(response);
  }
});

like image 39
timfjord Avatar answered Sep 20 '22 21:09

timfjord