天天看點

sendData to ABAP backend via multiple form content type

使用multipart/form-data的content type通過 Javascript 向application server發送資料:

<html>
<head>
<script type="text/javascript">
if (!XMLHttpRequest.prototype.sendAsBinary) {
  XMLHttpRequest.prototype.sendAsBinary = function(sData) {
    var nBytes = sData.length, ui8Data = new Uint8Array(nBytes);
    for (var nIdx = 0; nIdx < nBytes; nIdx++) {
      ui8Data[nIdx] = sData.charCodeAt(nIdx) & 0xff;
    }
    this.send(ui8Data);
  };
}
var xmlhttp;
function GetXmlHttpObject()
{
    if (window.XMLHttpRequest) {
       return new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
       return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}
function stateChanged()
{
   if (xmlhttp.readyState == 4) {
       document.getElementById("result").innerHTML = xmlhttp.responseText;
       document.getElementById("result").style.border = "1px solid #A5ACB2";
  }
}
function getRequestURL(str)
{
    var url = "<host:port>" + str;
    url = url + "&sid=" + Math.random();
    return url;
}
function showResult(str)
{
if (str.length == 0 ) {
  document.getElementById("result").innerHTML = "";
  document.getElementById("result").style.border = "0px";
  return;
}
xmlhttp = GetXmlHttpObject();
if (xmlhttp == null ){
  alert ("Your browser does not support XML HTTP Request");
  return;
  }
var requesturl = getRequestURL(str);
xmlhttp.onreadystatechange = stateChanged ;
if ("withCredentials" in xmlhttp) {
 console.log("Great!!!!!");
}
xmlhttp.open("POST",requesturl,true);
xmlhttp.setRequestHeader("Content-Type", "multipart\/form-data");
var data = [];
var firstName = "firstName";
var firstNameValue = "Jerry";
var lastName = "lastName";
var lastNameValue = "Wang";
data.push( "Content-Disposition: form-data; name=\"" + firstName + "\"\r\n\r\n" + firstNameValue + "\r\n"); 
data.push( "Content-Disposition: form-data; name=\"" + lastName + "\"\r\n\r\n" + lastNameValue + "\r\n"); 
var sBoundary = "---------------------------" + Date.now().toString(16);
xmlhttp.setRequestHeader("Content-Type", "multipart\/form-data; boundary=" + sBoundary);
var sendStream = "--" + sBoundary + "\r\n" + data.join("--" + sBoundary + "\r\n") + "--" + sBoundary + "--\r\n";
xmlhttp.sendAsBinary(sendStream);
}
</script>
</head>
<body>
<input type="text" id="fname" onkeyup="showResult(this.value)" />
<div id = "result" ></div>
</body>
</html>      
sendData to ABAP backend via multiple form content type
sendData to ABAP backend via multiple form content type
sendData to ABAP backend via multiple form content type
sendData to ABAP backend via multiple form content type

繼續閱讀