前台ajax发送数据给后台node,那后台node接收到数据时如何发数据给ajax中的success函数呢?新手一枚,求高手指点。
发布于 6 年前 作者 ailuhaosi 2863 次浏览 来自 问答

//前台Jquery代码:发送ajax请求 使用post方式发送json字符串给后台login $.ajax({ type: “post”, url: “http://localhost:3000/process”, dataType: “json”, data: { username: username, password: password } //, success: function(data) {//这个函数一直进不来 console.log(“hello world!”);//这个函数一直进不来 //接受返回的数据,前端判断采取的动作 if (data) { if (data.message == “false”) { alert(‘密码错误,请重新输入’); } else { alert(‘登陆成功’); } } } });

	//后台代码
	app.post('/process', function (req, res) { 
	 // 输出 JSON 格式

response = { first_name:req.body.usename, last_name:req.body.password }; console.log(response); res.send(JSON.stringify(response)); });

3 回复

success api请求成功自动会走。你这应该是请求失败了吧。

请求成功的, res.send(JSON.stringify(response));这一句执行了,返回页面了。

终于发现了,是success没加逗号。

回到顶部