Tag: ajax

在Chrome中发生Sequelize查询和PUT错误

学生问题! 我正在学习使用Sequelize ORM的Node.js / Express和MySQL数据库。 传统上,在我们简单的应用程序中,在使用Sequelize查询MySQL数据库之后,我们将在Express PUT路由中发出res.redirect('/') ,然后类似如下: app.post("/", function (req, res) { db.Burgers.create({ burger_name: req.body.burger_name }).then(function () { res.redirect('/'); }); }); 当使用findOrCreate()方法创build一个sequelize查询时,我遇到了一个问题。 也就是说,我正在努力寻找将res.redirect语句放在AJAX PUT请求上的位置。 出于某种原因,当我在PUT语句的快速path中附加了res.redirect('/')时,Chromenetworking检查器中将看到重复的PUT请求。 第一个PUT请求显示为(localhost:3000 / devour,input:text / plain,status:302)。 Express服务器接收到PUT请求,并且继续查询成功,更新MySQL数据库中正确的表。 但是,Express路由redirect不成功,Chrome Inpector显示错误“PUT http:// localhost: 3000/404(Not Found)”,当我看到networking选项卡时,我看到第二个PUT请求(localhost: 3000 /,input:xhr,status:404)。 这是Express PUT路线: app.put("/devour", function (req, res) { var customerId; // Check to see if the […]

Express res.renderredirect后无法工作

我正在使用Ajax向我的服务器发出PUT http请求。 我在/contact/:id'处理请求,然后redirect到'/contacts' 。 然而,redirect后,视图contacts/index不渲染,但当我只是去浏览器,并访问http://localhost:3000/contacts ,它完美的作品。 当我做console.log(params)在/contacts路线,他们是正确的,因为我希望是。 提前致谢! app.js: var path = require('path'); var express = require('express'); var load = require('consign'); var bodyParser = require('body-parser'); var cookieParser = require('cookie-parser'); var session = require('express-session'); var app = express(); app.set('view engine', 'ejs'); app.use(cookieParser()); app.use(session({secret: 'ntalk', resave: false, saveUninitialized: true})); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use('/public', express.static('public')); app.listen(3000, […]

如何从Node.js中的路由查询string获取数据与Ajax?

就像我的标题所说,我试图从包含json数据的AJAX请求('/ home / playback / json')的路由中提取数据。 但是这个数据将不存在,除非一个查询完成但我试图从我的客户端页面(playback.ejs)合并这个,但我不能连接到我的路线连接到我的数据库的查询。 任何想法或文件,我应该尝试? AJAX请求 $(document).ready(function () { $('#search').on('submit', function() { $.ajax({ url: "http://localhost:8080/home/playback/json", contentType: "application/json", dataType: 'json', success: function (data) { console.log(data); },error: function () { alert('error'); }, type: "GET", }); }); }); Route.js app.get('/home/playback', function (req, res) { res.render('playback.ejs'); }); app.get('/home/playback/json', function (req, res) { //database connection dbconnection.query("SELECT * […]

将$ ajax转换为PUT请求的提取()

我试图翻译下面的jQuery代码来使用抓取API。 它发出一个PUT请求: function save () { $.ajax('/{{user.username}}', { method: 'PUT', data: { street: $('#street').val(), city: $('#city').val(), state: $('#state').val(), zip: $('#zip').val() }, complete: function () { cancel() location.reload() } }) } 这是抓取API请求: fetch('/{{user.username}}', { method: 'PUT', headers: { 'Content-Type': 'application.json' }, body: JSON.stringify({ street: document.getElementById("street").value, city: document.getElementById("city").value, state: document.getElementById("state").value, zip: document.getElementById("zip").value }) }).then(() => { cancel() […]

Node.js AJAX删除403禁止

我尝试创build从服务器删除图像的方法。 现在我只想处理DELETE请求,并显示在控制台filePath …服务器不处理我的请求,并发送: jquery.min.js:4 DELETE http:// localhost:3000 / api / imagesbg / wedding-stock-2.jpg 403(禁止) 我做错了什么? 前(handlebars.js) {{#each images}} <div class="row"> {{#each this}} <div class="col-sm-4"> <img src="/images/background-slider/{{this}}" class="img-responsive img-thumbnail" alt="{{this}}"> <button type="button" class="btn btn-danger pull-right imgBgDel" file-name="{{this}}" > Delete </button> </div> {{/each}} </div></br> {{/each}} 脚本 $(function(){ $('.delete-msg').hide(); var url; $(".imgBgDel").on("click", function(){ var url = 'http://localhost:3000/api/imagesbg/' + […]

CORS问题从React应用程序>节点服务器>redirect到Google OAuth2身份validation发出AJAX请求

嗨,我一直在敲我的头研究这个过去2天没有运气。 这是我从React应用程序@localhost:3000尝试使用Google Oauth2 Passport策略进行身份validation时遇到的错误。 我运行一个单独的应用程序与本地主机上的节点/快递服务器:3001。 XMLHttpRequest无法加载http:localhost:3001 / api / auth / google / login。 从“http:localhost:3001 / api / auth / google / login”redirect到“https:accounts.google.com/o/oauth2/v2/auth?response_type = code&redirect_uri = http%3A%2F%2Flocalhost%3A3001%2Fapi %2Fauth%2Fgoogle%2Fcallback&scope = https%3A%2F%2Fmail.google.com&client_id = ***。apps.googleusercontent.com'has been blocked by CORS policy:No'Access-Control-Allow-Origin'header is present on请求的资源。 原因'http:localhost:3000'因此不被允许访问。 createError.js:16 未被捕获(承诺)错误:在XMLHttpRequest.handleError的createError(createError.js:16)处的networking错误(xhr.js:87) 这是在我的客户端使用的代码尝试从我的组件之一login: //button <div> <button className="btn btn-danger" onClick={props.googleAuth}> Login With Google </button> </div> […]

node.js使用Ajax读取二进制数据

在BE服务器中,我发送binarystring到FE服务器。 Binarystring在responseText中。 //So. I do this.\ xhr2.open('GET', url, true); xhr2.onload = function() { routeResponse = Buffer.from(xhr2.responseText, 'binary'); //init Buf = ''; //byte by byte output for (let i = 0; i < routeResponse.length; i++) { Buf += routeResponse.readUInt8(i).toString(16).toUpperCase(); Buf += ' '; } console.log(Buf); } 但是在日志和原始的cgi文件中是不同的二进制数据。 左:console.log(Buf)/右:原始文件中的hex值。 ex)getRoute.cgi 奇怪的是,只有某些值被输出为“FD”。 实际数据是'8B','8C'等等。 CGI文件格式:二进制/ Little Endian。 为什么某些数据被replace为“FD”请为我回答。 谢谢。

如何在nodejs中处理同一个get函数的多个调用?

使用Ajax调用我每10秒钟调用下面的get函数,以便我可以监视一些URL的状态。 app.get('/getUrl', function(req, res) { var response = {}; var keyArr = []; var urlData = []; var responses = []; var dataArr = []; var keyArrLength; //Listing All the URLS db.list(function(err, data) { keyArrLength = data.rows.length; dataArr = data.rows; console.log("keyArrLength" + keyArrLength); pushHost(); }); //Getting the key ie HOSTNAMES function pushHost() { dataArr.forEach(function(arrayItem) { […]

如何从客户端触发对nodejs服务器的调用javascript

新的networking编程。 对不起,可能没有问题。 我正在使用nodejsbuild立一个网站。 我想要做的是:每当页面刷新(或整个页面呈现 – 我将使用window.onload ),我想从我的HTML文件中的Javascript脚本触发一个调用到我的nodejs服务器通知它。 服务器将执行一些数据库操作等。 我需要了解如何做到这一点。 套接字或AJAX调用? 如果Ajax调用,我在哪里定义路由/函数来处理这个调用? 如果我在我的routes.js文件中定义它,它是否可访问?

NodeJS,Express显示来自AJAX POST的HTML页面响应

将NodeJS与Express服务器一起使用时,当您转到http:// localhost:(Serverport)时 , NodeJS服务器会使用以下代码发送HTML文件进行响应: app.get('/', function(req, res){ res.sendFile(__dirname + '/login.html'); }); 现在我正在使用JQuery AJAX POST向服务器发送信息,并根据服务器响应将用户发送到HTML页面“/ index”,或者如果用户凭据不好,HTML页面“/ loginno”。 问题是AJAX不像在服务器获取函数响应中一样玩sendfile的服务器响应。 我从服务器获取文件,并在控制台中输出完整的HTML,但我不知道如何使浏览器以与服务器GET响应相同的方式进入页面。 这是我的Ajax函数,它可以从服务器获取HTML页面对象,但浏览器不会导航到页面。 $.ajax({ type: "POST", url: '/index', //A string containing the URL to which the request is sent. timeout: 2000, //A plain object or string that is sent to the server with the request. data: userdata, //The type […]