服务器错误直接使用Javascript发布MIME多部分数据
我想直接在Javascript中建立多部分表单数据以便将我的数据发送到服务器。我知道有Ajax表单插件,但我真的认为它们不适合我的需求,因为我将在浏览器中创建二进制数据,并将它发送为文件提交(我将发布的服务器要求这样)。现在服务器错误直接使用Javascript发布MIME多部分数据
我的问题是,建筑文本多部分MIME数据的最简单的例子,在服务器端失败,出现错误:
500 Internal Server Error: Invalid boundary in multipart form
我试图代码减少到最低限度:在这个主。 html(这是它将在以后的服务器代码中引用的名称),这两个html表单都提交html-non-Ajax方式的文本,并且还有一个Javascript函数,它试图用XmlHttprequest复制该文本:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Posting MIME Multipart directly in Javascript</title>
<script>
function sendMimeMultipart(url, data) {
boundary = '---------------------------1504702169761927311267328916'
xhr = new XMLHttpRequest();
xhr.open("POST", url);
//Build the MIME POST request.
var body = "--" + boundary + "\r\n";
body += 'Content-Disposition: form-data; name="contents"\r\n\r\n';
body += data+"\r\n";
body += "--" + boundary + "--"+"\r\n";
var fileSize = body.length
xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary);
xhr.setRequestHeader("Content-Length", fileSize);
xhr.send(body);
return true;
}
function sendData() {
sendMimeMultipart('http://localhost:8080/myhandler', "Hello World!");
}
</script>
</head>
<body onload='sendData()'>
<form action = "myhandler" method = "post" enctype = "multipart/form-data">
<input type = "text" name = "contents">
<input type = "submit">
</form>
</body>
</html>
这是请求对象到达服务器使用的形式时:
Request: POST /myhandler
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Connection: keep-alive
Content-Length: 187
Content-Type: multipart/form-data;
boundary=---------------------------18171295601131570933197493099
Host: localhost:8080
Keep-Alive: 115
Referer: http://localhost:8080/
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; es-ES; rv:1.9.2.20)
Gecko/20110803 Firefox/3.6.20
-----------------------------18171295601131570933197493099
Content-Disposition: form-data; name="contents"
Hello World!
-----------------------------18171295601131570933197493099--
而这使用Javascript函数(sendMimeMultipart)时到达的到服务器的请求对象:
Request: POST /myhandler
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 185
Content-Type: multipart/form-data; charset=UTF-8,
boundary=---------------------------1504702169761927311267328916
Host: localhost:8080
Keep-Alive: 115
Pragma: no-cache
Referer: http://localhost:8080/
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; es-ES; rv:1.9.2.20)
Gecko/20110803 Firefox/3.6.20
-----------------------------1504702169761927311267328916
Content-Disposition: form-data; name="contents"
Hello World!
-----------------------------1504702169761927311267328916--
的2个字节中的Content-Length的差异是由于浏览器随机生成边界,有时更长,有时更短。在这种情况下,它是一个字符的长度,这是两个边界事件中两个字节差异的原因。
我不认为服务器有这么多,总线,以防万一我张贴服务器端代码。这是Appengine片段,仅供本地主机使用;对“localhost:8080/myhandler”的调用将检索浏览器发布的“contents”的值,并将其存储在全局变量中。之后,调用“localhost:8080/show”将显示先前检索的文本。正如我之前提到的,如果我们使用表单发送数据,文本内容将被正确保存,并且“show”处理程序将显示它。但是,如果我们使用Javascript,代码行:
contents = self.request.get("contents")
在MyHandler(代码如下)中,会产生错误。
这里是服务器代码:
import cgi
import datetime
import logging
import os
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import images
from google.appengine.ext.webapp import template
from os import environ
contents=''
class mein(webapp.RequestHandler):
def get(self):
template_values = {}
path = os.path.join(os.path.dirname(__file__), 'templates/main.html')
self.response.out.write(template.render(path, template_values))
class MyHandler(webapp.RequestHandler):
def post(self):
global contents
contents = self.request.get("contents")
class Show(webapp.RequestHandler):
def get(self):
global contents
self.response.headers['Content-Type'] = "text/plain"
self.response.out.write(contents)
application = webapp.WSGIApplication([
('/', mein),
('/myhandler', MyHandler),
('/show', Show)
], debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()
为什么这应该没有任何想法?我尝试了数十万种不同的东西,但我似乎无法使其工作或理解为什么它不行!
非常感谢您的想法和帮助。
所有最好的:
-Javier
我遇到试图手动构建一个HTTP文件上传时相同的错误消息。我通过在Content-Type标题中用分号(;)替换逗号(,)来实现它。在你的情况下,通过更换:
xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary);
有:
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary="+boundary);
这似乎是因为我使用Django(Python)的这个同样的问题,可能与Python的后端,当我调试它反对一个PHP测试服务器,逗号和分号都起作用。
最后,RFC1867的例子最好使用逗号,所以最终我不确定真正的解决方法,但是分号解决了它。
你有没有尝试从'Content-Type'中删除'charset'参数?在这种情况下没有意义。 –
我不知道如何摆脱它。包含它的是Javascript。 :-(如果你看到接受的字符集字段,它看起来像utf-8不应该是一个问题,这个测试仅用于文本内容,而不是二进制文件 –
多部分信息本身并不是真正的文本,但在某种意义上那是在那里使用的 - 而且每个部分都有自己的内容类型,但是,这似乎不太可能是这个问题 –