I am successfully calling a server side function from the client side with Ajax, but i cant figure out how to pass an array with the function call. As you can see, i am trying to get data out of the data tag on the server side but i am not getting the values i passed.
我使用Ajax从客户端成功调用服务器端函数,但我无法弄清楚如何通过函数调用传递数组。正如你所看到的,我试图从服务器端的数据标签中获取数据,但我没有得到我通过的值。
How do i get the "hi", "hello" text passed to the server function?
如何将“hi”,“hello”文本传递给服务器函数?
Client Side Ajax Calling Function:
客户端Ajax调用功能:
function ClientSide()
{
var info = [];
info[0] = 'hi';
info[1] = 'hello';
var json = JSON.stringify(info); //pass this
$.ajax({
type: 'post',
url: '/save',
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (html) {
// use data
}
})
}
Server Side Function:
服务器端功能:
app.post('/save', function(req,res,data)
{
var Passed_value = data;
console.log(Passed_value);
});
1 个解决方案
#1
2
First... your data is stored in req.body, not in third argument that you added.
首先......您的数据存储在req.body中,而不是存储在您添加的第三个参数中。
Second... if you are getting JSON in string, you got to parse it before using it as an object...
第二......如果你在字符串中获得JSON,你必须在将它用作对象之前解析它...
Code:
码:
app.post('/save', function(req,res)
{
var Passed_value = JSON.parse(req.body);
console.log(Passed_value);
});
#1
2
First... your data is stored in req.body, not in third argument that you added.
首先......您的数据存储在req.body中,而不是存储在您添加的第三个参数中。
Second... if you are getting JSON in string, you got to parse it before using it as an object...
第二......如果你在字符串中获得JSON,你必须在将它用作对象之前解析它...
Code:
码:
app.post('/save', function(req,res)
{
var Passed_value = JSON.parse(req.body);
console.log(Passed_value);
});