如何在JQuery中使用JSFiddle echofunction?
我已经阅读了jsFiddle用户指南,了解他们的JSON echofunction,但是没有运气生成一个有效的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 }
提供的一个例子是我从未使用过的Mootools。 因此,从mootools示例到JQuery的简单转换就足够了。
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); } });
现场演示
注意我添加了na additonal资源.. jquery-json
在View上使用FireBug控制台进行演示 (无需启动开发人员控制台即可查看返回)
您也可以使用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); } });
像这样的东西:
$.get('/echo/jsonp/', { foo : 'bar', biz : 'buzz' }) .success(function(data){ console.log (data) })
JS小提琴的例子 。
基本上,将$.ajax
函数的url
部分指向/echo/jsonp/
,您应该设置。 JSFiddle的文档说/echo/json/
可以工作,但看起来特定的URL目前已经关闭了; 但是,使用JSONP服务而不指定回调工作正常。