如何在JQuery中使用JSFiddle echo功能?

kas*_*ega 20 jquery json jsfiddle

我已经阅读了jsFiddle用户指南,了解他们的JSON echo功能,但没有运气生成一个有效的jsFiddle来使用JQuery回显JSON消息.

如何创建一个jsFiddle来回应他们的指南中的JSON:

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

提供的一个例子是我从未使用过的Mootools.因此,从mootools示例到JQuery的简单转换就足够了.

Baz*_*nga 24

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);
    }
});
Run Code Online (Sandbox Code Playgroud)

现场演示

注意我添加了na additonal资源.. jquery-json

在View上使用FireBug控制台进行演示(无需启动开发人员控制台即可查看返回)


tim*_*sly 13

您也可以使用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);
  }
});
Run Code Online (Sandbox Code Playgroud)

  • +1; "也"你的意思是"必须"?http://stackoverflow.com/questions/7759619/getting-this-error-tojson-is-not-a-function (4认同)
  • 从理论上讲,你可以准备自己的stringify函数,所以也许最好使用'also' (2认同)

ajm*_*ajm 6

像这样的东西:

$.get('/echo/jsonp/', { foo : 'bar', biz : 'buzz' })
    .success(function(data){ console.log (data) })
Run Code Online (Sandbox Code Playgroud)

JS小提琴的例子.

基本上,指向函数的url一部分,你应该设置.JSFiddle的文档说它也有效,但看起来这个特定的URL现在已经失效了; 但是,使用JSONP服务而不指定回调工作正常.$.ajax/echo/jsonp//echo/json/