我正在尝试将对象(JSON数组)发送到函数中的xmlHttprequest。如果我在body
函数内部对getToteMatrix
变量进行了硬编码,则它可以工作。但是,当我尝试从函数外部将body
变量传递给函数时,出现500错误。
getToteMatrix(body);
var body = '{"Action":"<must be non blank>", "SubAction":"", "Cart":"", "CartPositions":1,"OverallTC":false, "ErrorMessage":"","UserResponse1":"","UserResponse2":"","UserResponse3":"","DisplayData":[{"Part":"", "PartDesc1":"", "PartDesc2":"", "UserField":"", "Location":"", "DirectionalDisplay":"", "TotalPickQty":"5", "StartingAisle":"","FullTote":"", "FullToteQty":"", "NewToteQty":""} ],"BatchData":[{"RemainingPickLines":0, "RemainingLocs":0, "CurrentPicksPerHourRate":0.000}],"AlphaPos":[{"Pos": 1, "PrimaryModule":1, "SecondaryModule":3, "Message":"1111", "Color": 1, "BlinkSpeed": 0, "Buzzer": true, "Enable":true},{"Pos": 2, "PrimaryModule":2, "SecondaryModule":4, "Message":"2222", "Color": 1, "BlinkSpeed": 0, "Buzzer": false, "Enable":true}],"BatchPos": [{"Pos": 15, "PrimaryModule":1, "SecondaryModule":51, "Display":"*15*", "TC":false, "Color":1, "BlinkSpeed":0, "Enable":true, "Visible":true}, {"Pos": 2, "PrimaryModule":2, "SecondaryModule":52, "Display":"2222", "TC":false, "Color":1, "BlinkSpeed":0, "Enable":true, "Visible":true}, {"Pos": 5, "PrimaryModule":5, "SecondaryModule":55, "Display":"FIVE", "TC":false, "Color":1, "BlinkSpeed":0, "Enable":true, "Visible":true},{"Pos": 3, "PrimaryModule":3, "SecondaryModule":53, "Display":"3333", "TC":false, "Color":1, "BlinkSpeed":0, "Enable":true, "Visible":false},{"Pos": 4, "PrimaryModule":4, "SecondaryModule":54, "Display":"4444", "TC":false, "Color":1, "BlinkSpeed":0, "Enable":true, "Visible":true}]}';
function getToteMatrix(body) {
// when the body variable is declared here the xml request works just fine.
// var body = (above json)
var xmlhttp = new XMLHttpRequest();
var url = "http://promat.dovetree.com/cart/cart/batchpos";
xmlhttp.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
document.getElementById("id01").innerHTML = this.responseText;
}
};
xmlhttp.open("POST", url, true);
xmlhttp.send(body);
}
该函数应采用从服务器推送的HTML格式的数据和输出。在函数外声明的变量出现500错误。
答案 0 :(得分:0)
我认为您会收到500错误,因为调用getToteMatrix()
作为参数body
的{{1}}的时间是 undefined 。
由于起重,JavaScript编译器读取了这样的变量:
body
函数声明在变量声明之前。
检查以下示例:
getToteMatrix // function reference
anotherBody
body
时,getToteMatrix(body)
未定义。body
时,我们在调用之前声明了getToteMatrix(anotherBody)
,因此它将在控制台中打印出来。
anotherBody
答案 1 :(得分:0)
function getToteMatrix(body) {
console.log(body);
}
// first call
var body = 'text';
getToteMatrix(body);
// second call
var anotherBody = 'anotherBody';
getToteMatrix(anotherBody);
在这里您需要这样调用您需要先编写一个函数,然后调用一个Function,否则您将无法获得正确的答案。