2014-02-28 26 views
0

我试图创建的NodeJS从字符串文本文件,并发送回客户端

var text={"hello.txt":"Hello World!","bye.txt":"Goodbye Cruel World!"}; 
app.get('/files/:name',function(req,res){ 
res.set("Content-Type","text/plain"); 
res.send(text[req.params.name]); 
}); 

,这是我的客户端AJAX调用

$.ajax({ 
        type: verb || 'GET', 
        dataType: "text", 
        url: url, 
        data: data, 
        contentType : 'text/plain', 
        success: function() { $('div#callForm button').removeAttr('disabled'); }, 
        error: alert 
       }); 

但是没有文件返回下载。

有人能指出我正确的方向吗?

回答

1

看来你并没有接受客户端的回应。回调jQuery.ajaxsuccess传递3个参数:数据,状态和xhr(或您选择的任何变量名称)。

$.ajax({ 
    type: verb || 'GET', 
    dataType: 'text', 
    url: url, 
    data: data, 
    contentType : 'text/plain', 
    success: function (data, status, xhr) { 
    console.log(data, status); 
    $('div#callForm button').removeAttr('disabled'); 
    }, 
    error: alert 
}); 
+0

我看到生病添加这块到我的代码,看看是否得到一个文件后下载。现在标记正确谢谢 –

相关问题