2015-11-21 102 views
0

我使用標準的jQuery ajax POST請求從我的Chrome擴展中發佈數據到我的服務器。但是,我希望服務器在收到POST請求後,將一個JavaScript文件發送給Chrome擴展。我該怎麼做?我是否必須設置新的POST請求服務器端?或者我可以使用'回覆'發回一些內容嗎?收到POST請求後發回數據?

Client.js

request("https://61d6b1ac.ngrok.io/api/panels/hitme", "post", "JSON", {apples}) 
    .done(function(res){ 

    }) 
     }) 
    function request(url, method, dataType, data){ 
    return $.ajax({ 
    url: url, 
    method: method, 
    dataType: dataType, 
    data: data 
    }) 
    } 

Server.js

function sendJSfile(req, res) { 
    var body = req.body.apples 
    console.log(body) 

    if (body.length >= 3) { 
     console.log("Long!") 
    } else{ 
     console.log("Short!") 
     res.writeHead(200, {"Content-Type": "text/javascript"}); 
     fs.createReadStream("./example.js").pipe(res); 
    } 
    } 

回答

0

你可以只回聲/寫/把它拿出來,從服務器端。

$.post('somurl',{},function(returnData){ 
    //do something with returnData 
}); 

儘管強烈建議不要將javascript注入dom動態。你將不得不使用像eval()這樣的函數來超越它,這會讓你面對各種麻煩。

+0

我已經用代碼修改了我的問題。我不明白如何從服務器端放出來?我的代碼目前設置爲這樣做,但它不會將example.js發送到客戶端。沒有錯誤出現,但example.js不運行? – PickMeUpLittle