nodejs 使用 https 将资源发送回客户端

问题描述 投票:0回答:1

app.post(“/天气”,函数(请求,响应){

let city = request.body.city;

const options = {
    method: 'GET',
    hostname: 'open-weather13.p.rapidapi.com',
    port: null,
    path: "/city/" + city + "/EN",
    headers: {
        'x-rapidapi-key': '-----',
        'x-rapidapi-host': '-----'
    }
};

const req = http.request(options, function (res) {
    const chunks = [];

    res.on('data', function (chunk) {
        chunks.push(chunk);
    });

    res.on('end', function () {
        const body = Buffer.concat(chunks);
        console.log(body.toString());
    });

    res.send({ test: 'test' });

});

req.end();

});

我得到以下信息

res.send is not a function

这是我的ajax

          $(document).ready(function () {
  $("form").submit(function (event) {
      
    var formData = {
      city: $("#city").val(),
    };

    $.ajax({
      type: "POST",
      url: "http://localhost:8002/weather",
      data: formData,
      dataType: "json",
      encode: true,
          success: function (data, status, xhr) {
    console.log('data: ', data);
  }
    }).done(function (data) {
      console.log(data);
    });

    event.preventDefault();
  });
});
node.js ajax
1个回答
0
投票

res
http
请求的事件,它没有该方法,所以你实际上需要使用
express
's/
app
's
response
来代替:

app.post("/weather", function(request,response) {
   //...
   // now send response here
   response.send({ test: 'test' });
© www.soinside.com 2019 - 2024. All rights reserved.