我对下面的代码不感到困惑,我只是不明白这是哪种网址:/echo/json/
。
请参见this jsfiddle。
您可以看到数据已发布到此URL /echo/json/
,但该URL可能不存在。因此,请告诉我此网址/echo/json/
的工作方式。
$.ajax({
type: 'POST',
url: '/echo/json/',
data: {
json: ko.toJSON(entries),
delay: .1
},
success: function(entries) {
ko.utils.arrayForEach(entries, function(entry) {
viewModel.items.push(entry);
});
viewModel.pendingRequest(false);
},
error: function() {
viewModel.pendingRequest(false);
},
dataType: 'json'
});
我唯一的问题是有关此URL /echo/json/
的,我想知道它是如何工作的。
正如注释中指出的@nemesv,这是JSFiddle的功能。
The JSFiddle docs提供这些URL(/echo/html
,/echo/json
,/echo/jsonp
,/echo/xml
和/echo/js
)作为存根,它们仅回显给定数据。您可以使用它来模拟具有可选指定延迟的响应,例如,这对于测试不存在的REST操作或某种AJAX调用处理程序很有用。
从原始Javascript使用JSON URL的格式如下(从其示例页面中摘录:]
new Request.JSON({
url: '/echo/json/',
data: {
json: JSON.encode({
text: 'some text',
array: [1, 2, 'three'],
object: {
par1: 'another text',
par2: [3, 2, 'one'],
par3: {}
}
}),
delay: 3
},
onSuccess: function(response) {
show_response(response, $('post'));
}
}).send();