Although my expertise lies in C++, I must confess that my knowledge about web development is quite limited. Therefore, please bear in mind that my question requires a simple answer.
Recently, I stumbled upon a fascinating C++ library for creating a web server on Github, which can be found here.
To test its functionality, I entered http://localhost:8080/ into my browser and was pleased to see that it worked perfectly. I also tested:
http://localhost:8080/info
http://localhost:8080/match/8796
Both of them functioned flawlessly as well.
However, when attempting to test Ajax/Json features, I encountered an issue. Despite using the following code in my Firefox browser console:
$.post( "json", {firstName: "John",lastName: "Smith",age: 25} );
not well-formed json:1:18 ---> Could not open path /json
I also tried:
$.post( "string", {firstName: "John",lastName: "Smith",age: 25} );
But received a similar result.
Could you kindly point out where I might be making a mistake?
In essence, the C++ code hosts a server on port 8080 and responds to:
server.resource["^/string$"]["POST"]
server.resource["^/json$"]["POST"]
server.resource["^/info$"]["GET"]
server.resource["^/work$"]["GET"]
server.default_resource["GET"]
Below are some examples of client interactions:
//Client examples
HttpClient client("localhost:8080");
auto r1=client.request("GET", "/match/123");
cout << r1->content.rdbuf() << endl;
string json_string="{\"firstName\": \"John\",\"lastName\": \"Smith\",\"age\": 25}";
auto r2=client.request("POST", "/string", json_string);
cout << r2->content.rdbuf() << endl;
auto r3=client.request("POST", "/json", json_string);
cout << r3->content.rdbuf() << endl;
For those interested, you can access the `http_examples.cpp` file from this link.
#include "server_http.hpp"
#include "client_http.hpp"
//Added for the json-example
#define BOOST_SPIRIT_THREADSAFE
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
// ... (rest of the code omitted for brevity)