我想發送一條消息到服務器包含幾個不同的部分。目標是發送一些x-www-form-urlencoded信息與圖像。我試着做某事類同此:http://en.wikipedia.org/wiki/MIME#Multipart_messages在JavaScript中我的http請求有什麼問題?
這是我的js函數來做到這一點:
function sendPage() {
var source = document.getElementById("pageContainer")
var serializer = new XMLSerializer
if (!source.hasChildNodes()) {
alert("nie ma nic do wysłania")
return
}
var DOMNodeInString = "content=" + escape(serializer.serializeToString(source))
// sendToServer("savePage.php", true, handleAndShow, DOMNodeInString);return
xhttp.open("POST", "savePage.php", true)
var boundary = "xxx"
var body = "--" + boundary + "\r\n"
var file = document.getElementById("imgSource").files[0]
//wysyłam obrazek
if (file) {
var reader = new FileReader()
reader.readAsBinaryString(file)
body += "Content-Disposition: form-data; name='upload'; filename='" + file.name + "'\r\n"
body += "Content-Type: application/octet-stream\r\n\r\n"
body += reader.result + "\r\n"
body += "--" + boundary + "\r\n"
}
//wysyłam pozostałe pola formularza
body += "Content-Type: multipart/x-www-form-urlencoded \r\n\r\n"
body += DOMNodeInString
body += "\r\n--" + boundary + "--"
xhttp.setRequestHeader("Content-Type", "multipart/mixed; boundary=" + boundary)
xhttp.onreadystatechange = handleAndShow
alert(body)
xhttp.send(body)
}
然而,該功能不起作用。我的php腳本無法接收$ _POST [「content」]。我應該改變什麼來改進js腳本?
您是否嘗試過分析網絡Firebug(FF),Web Inspector(Chrome/Safari)還是Dragonfly(Opera)的活動?例如,響應標題是什麼? – 2010-11-14 12:59:44
Respose頭: 連接:保持活動 內容編碼:gzip 的Content-Length:20 的Content-Type:text/plain的 日期:孫老師,2010 11月14日17時03分52秒GMT 保持活動:超時= 15,max = 100 服務器:Apache/2.2.16(Debian) 不同:接受編碼 X Powered savePage.php是 <? header('Content-type:text/plain'); echo $ _POST [「content」]; ?> – 2010-11-14 16:04:50