原生JS AJAX上传

var pathName = window.document.location.pathname;
                var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
                var url = projectName+'/uploadFile/savePhoto';
                	
                /* 创建Ajax并提交 */
                    var xhr = new XMLHttpRequest(),
                        fd = new FormData(),
                        url = url;
                    fd.append('type', 'ajax');
                    fd.append('src', attrs['src']);
                    xhr.open("post", url, false);
                    xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
                    xhr.addEventListener('load', function (e) {
                    	var json = (new Function("return " + utils.trim(e.target.response)))();
                    	var attid = '';
                    	if(json.code=='0'){
                    		attid = json.data.att_map.ID;
                    		img.setAttr({
    	                        width:attrs.width,
    	                        height:attrs.height,
    	                        alt:attrs.alt,
    	                        word_img: attrs.src,
    	                        src:projectName+"/uploadFile/downloadFile?attachId="+attid
                    		})
                    	}
                    });
                    xhr.send(fd);

你可能感兴趣的:(JS)