2015-05-28 41 views
4

我对下面的代码没有困惑,我只是不明白这是什么样的url:/echo/json/这个jsfiddle中的url/echo/json /是做什么的?

请参阅this jsfiddle

您可以看到发布到此网址的数据/echo/json/但该网址可能不存在。所以告诉我这个网址/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' 
}); 

我唯一的问题是关于这个网址/echo/json/,我想知道它是如何工作的。

+8

这是的jsfiddle的一个特点:http://doc.jsfiddle.net/use/ echo.html – nemesv

+0

哦,对不起,我不知道 – Mou

回答

4

由于@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();