首页 > 解决方案 > 将python结果从节点js发送到ajax

问题描述

我在节点 js 服务器中有一个 python 函数。我正在尝试将 python 结果值从节点 js 服务器发送到 ajax。

const express = require('express');
const app = express();
const port = 8080;
var PythonShell = require('python-shell');

app.get('/', function(req, res) {
    var options = {
        mode: 'text',
        pythonPath:'',  
        pythonOptions:['-u'],
        scriptPath:'',
        args: [req.query.country]  
    };
    PythonShell.PythonShell.run('test.py', options, function(err, results) {
        if(err) throw err;
        console.log('results: ', results);
        res.send(results.toString());
    });
    res.sendFile(__dirname + '/search.html');
});
$.ajax({
    type: "GET",
    url: 'http://localhost:8080/',
    data: {country:country},
    dataType:'text',
    success: function(data) {
        console.log('data: ', data);
    }
});

我不知道应该使用哪种方法将结果发送到 ajax,并且我不确定来自 ajax 的“数据”变量是否适合用作来自服务器端的结果。

有人有什么主意吗?

标签: javascriptpythonnode.jsajax

解决方案


推荐阅读