[英]PHP Using both client side and server side validation WITHOUT using 3rd party code
[英]Add a script using fetch to get data by client side from a 3rd party, in express and ejs templating
我的 Express 應用程序使用 ejs 作為模板向客戶端呈現 html。
我試圖讓客戶端使用本機“獲取”方法向第三方 api 發送 http get 請求。 但客戶端必須使用我的應用程序中的數據來滿足請求。
現在,如果我將 function 放入 ejs <%%> 標記內,我會在瀏覽器中收到錯誤消息“未定義獲取”。
請看看我的代碼。
我的 ejs 文件中的代碼
<%fetch("url"+name[0]).then(response => response.json()).then(%><%=response%><%)%>
在我的 app.js 中
res.render("index",{name:["jon","donald","jo"]})
您不需要完整的EJS 腳本。 必要時只需使用<script>
標記和 output EJS 變量。 我還看到您沒有輸出您在app.js
中輸入的變量,您必須這樣做才能使用您傳入的變量。
例子:
<script>
fetch("url<%= name[0] %>").then(response => response.json());
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.