我只是尝试在不使用 Angular、React 或其他框架库的情况下练习将数据从静态 html 页面操作到 Node.Js。我已经安装了所有依赖项,并且节点正在为我的 index.html 提供服务,但是,我似乎无法让我的 post 方法起作用。我什至无法将其写入 console.log 任何内容。我查看了其他堆栈溢出问题和文档,但似乎无法弄清楚问题所在。
这是我的代码和文件结构。
HTML:
<form id="citySearchForm" action="http://127.0.0.1:3000/getcity" method="POST">
<div>
<p>Choose a city:</p>
<input type="text" placeholder="Enter a city" id="getCitiesInput" name="city"></input>
<input type="submit" value="submit">
</div>
<div id="weather"></div>
<p><span id="temp"></span></p>
<p><span id="wind"></span></p>
</form>
Run Code Online (Sandbox Code Playgroud)
节点.js:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false});
app.use(express.static('public'));
app.get('/index.html', function(req, res){
res.sendFile(_dirname + "/" + "index.html");
})
app.post('/getcity', urlencodedParser, function(req, res){
response = { city : req.body.city }; …Run Code Online (Sandbox Code Playgroud) 我有一个现有的大型项目,该项目将转换为打字稿,并将最终进行重构,然后放入Angular。当前项目中包含很多Jquery。我在项目中实现了当前的@types。
问题是这条线
if (["Priority", "PM"].indexOf($('option:selected', this).val()) >= 0) {
Run Code Online (Sandbox Code Playgroud)
由于类型定义而创建错误。我得到[TS]类型为'string |的参数。编号 string []'不可分配给'string'类型的参数。因为indexOf定义为“(方法)Array.indexOf(searchElement:string,fromIndex ?: number):number”,而Jquery的.val被定义为“ JQuery.val():string | number | string [](+1超载)”。
我需要一种方法来覆盖.val定义,仅将其字符串化,以便我可以将其编译为类似这样的行,而不会覆盖整个项目的.val。我在项目中有很多这样的代码行,所以我还不能只重写每个代码行。最终,我们将完全摆脱Jquery。如果有人能指出我正确的方向,那就太好了。
我是Lumen的新手,只是试图创建一个应用程序.我收到一个错误,在Application-> Laravel\Lumen\Concerns {closure}(8,'Undefined variable:app','/ Users/test/Site/books /routes/web.php',14,array( 'router'=> object(Router)))当我尝试使用这段代码时:
$app->group(['prefix' => 'book/'], function() use ($app) {
$app->get('/','BooksController@index'); //get all the routes
$app->post('/','BooksController@store'); //store single route
$app->get('/{id}/', 'BooksController@show'); //get single route
$app->put('/{id}/','BooksController@update'); //update single route
$app->delete('/{id}/','BooksController@destroy'); //delete single route
});
Run Code Online (Sandbox Code Playgroud)
根据文件https://lumen.laravel.com/docs/5.5/routing这应该工作.我正在关注https://paulund.co.uk/creating-a-rest-api-with-lumen上的教程我知道5.5几天前就出来了,所以可能没有人知道答案但是,任何帮助将不胜感激.
我正在进行api调用并收到数据,但是,我无法将数据发送回我的js文件.我尝试使用res.send但是我收到了一个错误.我似乎无法弄清楚如何将信息发送回javascript文件.(我把我的密钥从请求链接中取出.但是出于安全原因,我从api调用中获取数据).我遇到的唯一问题是将数据返回到前端javascript文件.
这是发送原始请求的Javascript文件:
/ ********** options button function makes api call to get selected cities forecast *****************
function getCityForecast(e){
var id = document.getElementById('cities');
var getValue = id.options[id.selectedIndex].value;
var suffix = getValue + ".json";
var newObj = JSON.stringify({link : suffix});
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:3000/", true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(newObj);
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
console.log(xhr.response);
console.log('recieved');
} else {
console.log('error');
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的server.js文件如下所示:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var http = …Run Code Online (Sandbox Code Playgroud)