POST Request from Ajax Call

Oct 9, 2014 at 11:08 AM
Edited Oct 9, 2014 at 1:40 PM
I am using this source code to receive POST request using Ajax Call
listener.support(methods::POST,[](http_request req)
    {
        std::cout << "Serving POST" << std::endl;
        req.extract_string(true).then([req](utility::string_t body)
        {
            std::wcout << body << std::endl;            
        });
    }); 
This is what I am using in Ajax call
$(document).ready(function() {
    $('input[type="text"]').focus(function () {
            $(this).css({ 'background-color': 'lime' });
        });
        
        $('input[type="text"]').blur(function () {
            var color = $(this).css('background-color');
            var value=$(this).val();
            $.ajax({
                type: 'POST',
                url: "http://localhost:2001/",
                // data: { name: value, cc:color },
                data: { name: value},
                
                success: function () {
                      return false;
                }
            });
      $(this).css({'background-color' : 'white'});
        });  
   $('iframe').ready(
   function () { 
        $('iframe').attr('id', 'textframe');
        $('#textframe').css({'border': '3px dashed red' });            
    });
});
Well I am sending these requests from chrome and safari addins, where this contents.js is used to make agax call request.

The problem is, it is working in case of chrome addins but not with Safari addins. Safari has a URL Filter so I need to send special headers in order to allow the messages into the toolbar. Part of the cross-site scripting problems you were having.

So can someone tell me, how can I send those special headers, from my listener application?
Oct 9, 2014 at 5:27 PM
Hi maverick786us,

In your listener example you aren't replying to the request at all. You can specify HTTP headers by manually creating the http_response object. As far as Safari, I'm not very familiar with it and can't help you. You probably can find better information else where.

Steve
Oct 10, 2014 at 5:15 AM
Edited Oct 10, 2014 at 6:05 AM
This is the complete source code.
int _tmain(int argc, _TCHAR* argv[])
{
    uri_builder uri(L"http://localhost:2001/");
    http_listener listener(uri.to_uri());

    json::value postData;
    postData[L"Name"] = json::value::string(L"Joe Smith");

    std::string strName; 
    std::cout << "Welcome to Casablanca" << std::endl;

    listener.support(methods::GET, [](http_request req)
    {
        //utility::string_t strName1;
        //ucin >> strName1;

        //std::cout << "Serving GET" << std::endl;
        req.reply(status_codes::OK, U("Welcome to Casablanca"), U("text/html"));
        //req.reply(status_codes::OK, strName1, U("text/html"));
        //req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is a GET Request.)</body></html>"), U("text/html"));
    });

    listener.support(methods::PUT,[](http_request req)
    {
        std::cout << "Serving PUT" << std::endl;
        req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is a POST Request.)</body></html>"), 
            U("text/html"));
    });

    listener.support(methods::POST,[](http_request req)
    {
        std::cout << "Serving POST" << std::endl;
        req.extract_string(true).then([req](utility::string_t body)
        {
            std::wcout << body << std::endl;
            //req.reply(status_codes::OK, U("<html><body>Received.</body></html>"), U("text/html"));
        });
    }); 

    listener.open().wait();
    fgetc(stdin);
    listener.close().wait();
}
Can you tell me, how can I implement http headers?
Oct 10, 2014 at 7:12 AM
Oct 10, 2014 at 1:22 PM
Can't find much. But lets say, in above code I want to add these 2 headers

1) Access Control for Cross-Site Requests and
2) Access-Control-Allow-Origin: *

How can I accomplish this?