Skip to content

Commit c2db028

Browse files
author
zhaoliang
committed
添加新的文章node_upgrade/ajax form 提交
1 parent f0c6214 commit c2db028

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
搭建liquid代理服务器.md
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
layout: post
3+
title: Ajax form表单提交
4+
categories: js
5+
tags: ajax
6+
---
7+
8+
* content
9+
{:toc}
10+
11+
# 原生FormData对象使用
12+
13+
* HTML创建form对象
14+
```
15+
var formElement = document.querySelector("form");
16+
//或者这种写法
17+
//var formData = new FormData(someFormElement);
18+
var request = new XMLHttpRequest();
19+
request.open("POST", "submitform.php");
20+
request.send(new FormData(formElement));
21+
```
22+
* 创建FormData对象
23+
```js
24+
var formData = new FormData();
25+
26+
formData.append("username", "Groucho");
27+
formData.append("accountnum", 123456); // 数字 123456 会被立即转换成字符串 "123456"
28+
29+
// HTML 文件类型input,由用户选择
30+
formData.append("userfile", fileInputElement.files[0]);
31+
32+
// JavaScript file-like 对象
33+
var content = '<a id="a"><b id="b">hey!</b></a>'; // 新文件的正文...
34+
var blob = new Blob([content], { type: "text/xml"});
35+
36+
formData.append("webmasterfile", blob);
37+
38+
var request = new XMLHttpRequest();
39+
request.open("POST", "http://foo.com/submitform.php");
40+
request.send(formData);
41+
```
42+
43+
> (FormData 对象的字段类型可以是 Blob, File, 或者 string: 如果它的字段类型不是Blob也不是File,则会被转换成字符串类型。
44+
45+
# JQuery form回调函数提交
46+
```js
47+
// 不带文件上传的form回调提交
48+
$("#ajaxform").submit(function(e)
49+
{
50+
var postData = $(this).serializeArray();
51+
var formURL = $(this).attr("action");
52+
$.ajax(
53+
{
54+
url : formURL,
55+
type: "POST",
56+
data : postData,
57+
success:function(data, textStatus, jqXHR)
58+
{
59+
//data: return data from server
60+
},
61+
error: function(jqXHR, textStatus, errorThrown)
62+
{
63+
//if fails
64+
}
65+
});
66+
e.preventDefault(); //STOP default action
67+
e.unbind(); //unbind. to stop multiple form submit.
68+
});
69+
70+
$("#ajaxform").submit(); //Submit the FORM
71+
```
72+
73+
```js
74+
// 带文件上传的form回调提交
75+
$("#multiform").submit(function(e)
76+
{
77+
var formObj = $(this);
78+
var formURL = formObj.attr("action");
79+
var formData = new FormData(this);
80+
$.ajax({
81+
url: formURL,
82+
type: 'POST',
83+
data: formData,
84+
mimeType:"multipart/form-data",
85+
contentType: false,
86+
cache: false,
87+
processData:false,
88+
success: function(data, textStatus, jqXHR)
89+
{
90+
91+
},
92+
error: function(jqXHR, textStatus, errorThrown)
93+
{
94+
}
95+
});
96+
e.preventDefault(); //Prevent Default action.
97+
e.unbind();
98+
});
99+
$("#multiform").submit(); //Submit the form
100+
```
101+
# JQuery FormData对象上传文件
102+
103+
* HTML表单FormData初始化
104+
105+
```js
106+
$.ajax({
107+
url: 'php/upload.php',
108+
data: data,
109+
cache: false,
110+
contentType: false,
111+
processData: false,
112+
type: 'POST',
113+
success: function(data){
114+
alert(data);
115+
}
116+
});
117+
```
118+
> **注意**
119+
> *contentType:false* //强制JQuery不添加Content-Type头部,否则文件上传的分割线(boundary string)將丢失;
120+
> *processData:false* //如果设置为true JQuery则会將内容转换为string,这是错误的
121+
122+
123+
相关链接:
124+
[https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects](https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects)
125+
126+
[https://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax](https://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax)

_posts/js/2017-06-23-node_upgrade.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
layout: post
3+
title: Nodejs 更新
4+
categories: js
5+
tags: nodejs
6+
---
7+
8+
* content
9+
{:toc}
10+
11+
# 升级nodejs
12+
13+
* 检查当前nodejs版本
14+
```
15+
node -v
16+
```
17+
18+
* 清空npmcatch
19+
```
20+
sudo npm cache clean -f
21+
```
22+
23+
* 安装“n”
24+
```
25+
sudo npm install -g n
26+
```
27+
28+
* 安装指定版本
29+
```
30+
sudo n 0.8.11
31+
32+
sudo n stable
33+
```
34+
35+
* 检查你的版本
36+
```
37+
node -v
38+
```
39+
40+
[原文地址](http://theholmesoffice.com/node-js-fundamentals-how-to-upgrade-the-node-js-version/)

0 commit comments

Comments
 (0)