如何从跨域环境中的ajax调用调用nodejs API并通过将数据返回到Ajax来获取响应

时间:2022-03-27 07:37:33

Help me in this i m new in nodejs and API calling through Ajax

通过Ajax帮我解决nodejs和API中的新问题

//Node Function to be call via ajax 
app.get('/Employee', function (req, res) {
  var  obj = {};
       // res.sendFile(__dirname + "/" + "Employee.html");
    var mysql = "Select * from employeelogin";
    
        con.query(mysql, function (err, result) {
            if (err) throw err;
            console.log(result);//
            obj = result;

    });
//return Obj
    res.render('/Employee', obj);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
function Get_Emp_record() {
    $.ajax({
       // url: '/Employee/GetDetails',
        url:'http://127.0.0.1:8081/Employee',
        data: {},
        method: 'GET',
        async: false, 
        crossDomain: true,
        processData: true,
        contentType: "application/json",
        dataType: 'json',
        jsonpCallback: 'callback', 
        success: function (data) {
           
            alert(data)
        },
        error: function (jqXHR, textStatus, err) {
            //show error message
            alert('text status ' + textStatus + ', err ' + err)
        }
    });
}

Thank you in advance

先谢谢你

2 个解决方案

#1


1  

simple solution for this, would be to use "cors" module.

对此的简单解决方案是使用“cors”模块。

installation

安装

npm install cors

usage:

用法:

var cors = require('cors')
app.use(cors());

Thats all you need to do.

这就是你需要做的一切。

Note: some HTTP requests might be preflighted, "preflighted" requests first send an HTTP request by the OPTIONS method to the resource on the other domain, in order to determine whether the actual request is safe to send. To enable pre-flighting, you must add a new OPTIONS handler for the route you want to support:

注意:某些HTTP请求可能是预检的,“预检”请求首先通过OPTIONS方法向另一个域上的资源发送HTTP请求,以确定实际请求是否可以安全发送。要启用预先飞行,您必须为要支持的路由添加新的OPTIONS处理程序:

app.options('/route', cors()) // for a particular route

To enable pre-flighting for all routes just use:

要为所有路线启用预先飞行,只需使用:

app.options('*',cors()) //for all routes(include before other routes)

more on pre-flighted requests : https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests

更多关于预先发出的请求:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests

#2


1  

To elaborate vibhor1997a's comment,

详细说明vibhor1997a的评论,

Install cors package in your project by issuing the below command,

通过发出以下命令在项目中安装cors包,

$ npm install cors

You can include in your application like this,

您可以在此类申请中加入,

var express = require('express')

And you can enable the CORS like this,

你可以像这样启用CORS,

app.use(cors());
app.get('/Employee', cors(), function(req, res) {.....

Hope this helps!

希望这可以帮助!

#1


1  

simple solution for this, would be to use "cors" module.

对此的简单解决方案是使用“cors”模块。

installation

安装

npm install cors

usage:

用法:

var cors = require('cors')
app.use(cors());

Thats all you need to do.

这就是你需要做的一切。

Note: some HTTP requests might be preflighted, "preflighted" requests first send an HTTP request by the OPTIONS method to the resource on the other domain, in order to determine whether the actual request is safe to send. To enable pre-flighting, you must add a new OPTIONS handler for the route you want to support:

注意:某些HTTP请求可能是预检的,“预检”请求首先通过OPTIONS方法向另一个域上的资源发送HTTP请求,以确定实际请求是否可以安全发送。要启用预先飞行,您必须为要支持的路由添加新的OPTIONS处理程序:

app.options('/route', cors()) // for a particular route

To enable pre-flighting for all routes just use:

要为所有路线启用预先飞行,只需使用:

app.options('*',cors()) //for all routes(include before other routes)

more on pre-flighted requests : https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests

更多关于预先发出的请求:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Preflighted_requests

#2


1  

To elaborate vibhor1997a's comment,

详细说明vibhor1997a的评论,

Install cors package in your project by issuing the below command,

通过发出以下命令在项目中安装cors包,

$ npm install cors

You can include in your application like this,

您可以在此类申请中加入,

var express = require('express')

And you can enable the CORS like this,

你可以像这样启用CORS,

app.use(cors());
app.get('/Employee', cors(), function(req, res) {.....

Hope this helps!

希望这可以帮助!