用CppCMS实现web site并同时支持json_rpc服务
2011-09-08 16:00
417 查看
刘怀宇
1.将cppcms-0.99.10/examples/json_rpc/rpc.cpp文件中的json_service类拷贝到
examples/templates/hello-tmpl.cpp 中
2.修改hello-tmpl.cpp 中的main函数,添加两个服务
int main(int argc,char ** argv)
{
try {
cppcms::service srv(argc,argv);
srv.applications_pool().mount(cppcms::applications_factory<my_hello_world>() , cppcms::mount_point(std::string("/html")) );
srv.applications_pool().mount(cppcms::applications_factory<json_service>() , cppcms::mount_point(std::string("/jsonrpc")) );
srv.run();
}
catch(std::exception const &e) {
std::cerr<<e.what()<<std::endl;
}
}
3.修改config.js 文件,添加 script_names : /jsonrpc , /html
4.在first.tmpl 中使用如下的html文本 替换掉原来的html
5.make
6. 执行生成的hello-tmpl ./hello-tmpl -c config.js
7. 访问http://localhost:8080/html 在返回的html页面中计算除法,即可成功访问后台的json服务,得到结果
8.注意,IE6不支持JSON_RPC技术。
1.将cppcms-0.99.10/examples/json_rpc/rpc.cpp文件中的json_service类拷贝到
examples/templates/hello-tmpl.cpp 中
2.修改hello-tmpl.cpp 中的main函数,添加两个服务
int main(int argc,char ** argv)
{
try {
cppcms::service srv(argc,argv);
srv.applications_pool().mount(cppcms::applications_factory<my_hello_world>() , cppcms::mount_point(std::string("/html")) );
srv.applications_pool().mount(cppcms::applications_factory<json_service>() , cppcms::mount_point(std::string("/jsonrpc")) );
srv.run();
}
catch(std::exception const &e) {
std::cerr<<e.what()<<std::endl;
}
}
3.修改config.js 文件,添加 script_names : /jsonrpc , /html
{ "service" : { "api" : "http", "port" : 8080 }, "http" : { "script_names" : ["/jsonrpc","/html"] } }
4.在first.tmpl 中使用如下的html文本 替换掉原来的html
<html> <body> <script type="text/javascript"> function call() { var xhr = new XMLHttpRequest(); xhr.open("post", 'http://localhost:8080/jsonrpc'); // Required by JSON-RPC over HTTP xhr.setRequestHeader("Content-Type","application/json"); // It is better to use real formatter like JSON.js x=parseInt(document.getElementById('x').value); y=parseInt(document.getElementById('y').value); var request = '{"method":"div","params":[' + x + ',' + y +'],"id":1}'; xhr.onreadystatechange = function() { if (xhr.readyState === 4) { res = 'Unknown Error'; if(xhr.status === 200) { // Don't call eval in real code use some parser var result = eval('(' + xhr.responseText + ')'); if(result.error==null) { res = result.result; } else { res = result.error; } } document.getElementById('result').innerHTML = res; } } xhr.send(request); return false; } </script> <form onsubmit="return call();"> <p> <input type="text" id="x" /> <input type="submit" value="/" /> <input type="text" id="y" /> = <span id="result"></span> </p> </form> </body> </html>
5.make
6. 执行生成的hello-tmpl ./hello-tmpl -c config.js
7. 访问http://localhost:8080/html 在返回的html页面中计算除法,即可成功访问后台的json服务,得到结果
8.注意,IE6不支持JSON_RPC技术。
相关文章推荐
- 用CppCMS实现web site并同时支持json_rpc服务
- 用CppCMS实现web site并同时支持json_rpc服务
- 用CppCMS实现web site并同时支持json_rpc服务
- 使用cppcms开发JSON_RPC服务:高级用法
- pyjsonrpc+multiprocessing实现可并发处理RPC服务
- 【转】 在 ASP.NET 3.5 中使用同时支持 Ajax Json 和 .NET 客户端的 WCF 服务
- WebService、RMI、RPC、XML-RPC、JSON-RPC、SOAP、REST(rest-api、restful)等web服务实现方案概念
- 自定义JSP标签实现语言国际化(类似struts text标签),并同时支持图片、JS文件国际化
- 继承JsonConverter实现json.net支持dataset与datatable序列化
- 同时支持mybatis,hibernate等技术的通用持久层实现思路
- 利用SpringMVC实现基于Http和Json的轻量级RPC框架
- 使用JSON Web Tokens和Spring实现微服务
- 易用的C++ RPC服务框架 - pioneer - 5 - 技术实现:函数的序列化
- 设计并实现同时支持多种视频格式的流媒体点播系统
- 手机端可以和PC端同时在线-java QRCode 实现网站扫码登录(即支持同帐号多设备同时登录)
- SpringMVC同时支持多视图(JSP,Velocity,Freemarker等)的一种思路实现
- 用nodejs实现json和jsonp服务
- [grub实现]U盘引导多个linux镜像安装,同时支持BIOS和UEFI模式
- Thrift 个人实战--RPC服务的发布订阅实现(基于Zookeeper服务)