gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
前端页面 调用node起的服务
- User
前端页面 调用node起的服务
- Assistant
在前端页面中调用 Node.js 服务有多种方法,以下是其中两种:
- Ajax 请求
可以使用 Ajax 发送 HTTP 请求来调用 Node.js 服务。例如,使用 jQuery 的 Ajax 方法:
$.ajax({ url: 'http://localhost:3000/api/some-method', type: 'POST', dataType: 'json', data: {param1: 'value1', param2: 'value2'}, success: function(response) { console.log(response); }, error: function(xhr, status, error) { console.error(status + ': ' + error); } });需要注意的是,需要允许跨域访问,否则可能会遇到 CORS 问题。可以在 Node.js 服务端添加以下代码来允许跨域:
const express = require('express'); const app = express(); app.use((req, res, next) => { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); next(); }); // ...
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.