分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

2017.12.3 ?Ajax与React提交数据到指定服务器程序文件

发布时间:2023-09-06 01:28责任编辑:蔡小小关键词:AjaxReact
var Common ={ test:function (date) { ????var xhr=new XMLHttpRequest(); ???????xhr.onreadystatechange=function () { ???????????if(xhr.readyState==4){ ???????????????if((xhr.status>=200 && xhr.status<300 || xhr.status==304)){ ???????????????????alert(xhr.responseText); ???????????????}else{ ???????????????????alert("Request was unsuccessful:"+xhr.status); ???????????????} ???????????} ???????} ???????xhr.open("get","postTest.js",true); ???????xhr.setRequestHeader("Content-Type","multipart/form-data") ???????xhr.send(date); ???????alert(date); }};module.exports =Common;

postTest.js的内容:相当于一个服务器文件程序,监听前端的请求;

var http = require("http");var url = require("url");var querystring = require(‘querystring‘);http.createServer(function(request, response) { ???//request.setEncoding(‘utf8‘); ???response.writeHead(200, {"Content-Type": "text/plain"}); ???response.write("送一段数据给你!"); ???request.addListener(‘data‘, function(chunk){ ???????console.log(‘获取的post数据为:‘ + chunk); ???????var name = querystring.parse(chunk + ‘‘).name; ?//chunk是一个对象 加上空字符串将其转换为字符串格式 ???????console.log("--------------------"); ???????console.log(name); ???}) ???response.end();}).listen(8888)

3.解决Bug之Input组件获取值是空的(也就是Object)问题,只有Input组件需要这样:

查看返回结果使用:

console.log(data);          //尽量不要用alert

2017.12.3 ?Ajax与React提交数据到指定服务器程序文件

原文地址:http://www.cnblogs.com/hqutcy/p/7966102.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved