Invalid rhs Error When Sending 2D Array to Production Server

조회 수: 3 (최근 30일)
Zachary Khan
Zachary Khan 2021년 9월 25일
답변: Zachary Khan 2021년 9월 25일
Hi, I am attempting to send a simple 2D array to a function I have deployed on production server that expects a single input argument. I have the following JavaScript code (mostly copied from these docs (https://www.mathworks.com/help/mps/restfuljson/postasynchronousrequest.html)):
var data = JSON.stringify({
rhs: track,
"nargout": 1,
"outputFormat": {"mode": "large","nanType": "object"}
});
console.log(data);
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:9910/finalAnalysis/analysisV1");
xhr.setRequestHeader("content-type", "application/json");
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.send(data);
Where track is the array I want to send to the server.
Please note I have also attempted wrapping the track argument in brackets like so: rhs: [track], however I still get the same following error:
analysis.js:91 POST http://localhost:9910/finalAnalysis/analysisV1 400 (InvalidInputArgs Invalid rhs. (request id=0:134:7))
I've looked around the docs and I'm having difficulty figuring out how to properly format my rhs to solve this error. Can someone help me?

채택된 답변

Zachary Khan
Zachary Khan 2021년 9월 25일
I figured out the issue I was having. For anyone who is wondering, the track array I was passing in was an array of string values, when it needed to be an array of actual numbers, so I converted each of the strings to numbers using Number() in JavaScript. Here's the nested loop I wrote to do this, don't know if it's the best way but it works:
let track = new Array(trackStr.length);
for (i = 0; i < trackStr.length; i++) {
track[i] = new Array(trackStr[0].length); // Initialize an empty array of the correct dimensions
}
for (j = 0; j < trackStr[0].length; j++) {
for (i = 0; i < trackStr.length; i++) {
if (trackStr[i][j]) { // Check that this array index has a value that exists
track[i][j] = (Number(trackStr[i][j])); // Insert the numerical version of the string into the same position in the empty array. (Note Number method is used because it handles scientific notation values too)
}
}
}

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 RESTful API에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by