使用jQuery、Ajax和C++ Web服务器加载组合框
我尝试在使用jQuery和Ajax滚动时加载内容,但这段代码有问题 cppwebserver方法获取请求处理器和响应。该方法从课程表数据库中选择数据,并将结果转换为json。最后根据response.write返回结果使用jQuery、Ajax和C++ Web服务器加载组合框,jquery,c++,json,ajax,webserver,Jquery,C++,Json,Ajax,Webserver,我尝试在使用jQuery和Ajax滚动时加载内容,但这段代码有问题 cppwebserver方法获取请求处理器和响应。该方法从课程表数据库中选择数据,并将结果转换为json。最后根据response.write返回结果 void processGetRequest(const char* path, const char* get_content, IHTTPResponse& response) { if (strcmp(path, "/getCourseJson") ==
void processGetRequest(const char* path, const char* get_content, IHTTPResponse& response)
{
if (strcmp(path, "/getCourseJson") == 0)
{
QLParam Qparam[] =
{
SQL_INT, &CourseID, sizeof(int),
SQL_TEXT, Name, ARRAYSIZE(Name),
};
SQLReader sreader = mj_server->dbRead("SELECT CourseID, Name FROM Courses;");
std::map<std::string, std::string> mymap;
while (mj_server->dbRecord(sreader, Qparam, ARRAYSIZE(Qparam)))
{
std::string str = std::to_string(CourseID);
mymap.insert(std::pair<std::string, std::string>("ID", str));
mymap.insert(std::pair<std::string, std::string>("Name", Name));
}
mj_server->dbFinalize(sreader);
std::string mm = json::map_to_json(mymap);
const char* mzm = mm.c_str();
response.write(mzm);
}
}
在jquery中,我编写了一个简单的html文件,并将此脚本放入其中
$(document).ready(function () {
$.ajax({
type: "get",
url: "/getCourseJson",
datatype: "Json",
success: function (data) {
var s = '<option value="-1">Please Select a Course </option>';
for (var i = 0; i < data.length; i++) {
s += '<option value="' + data[i].ID + '">' + data[i].Name + '</option>';
}
$("#Course_sel").html(s);
}
});
});
现在我的问题是,当我运行程序时,CPP Web服务器接收get请求,结果被正确地发送到jquery,但是在jquery中,接收到的Json数据是逐字符处理的,而不是对象和元素,所以组合框中填充了未定义的文本
请帮我解决这个问题
谢谢各位