Best way to pass data to javascript

Do not post support requests, bug reports or feature requests. Discuss CEF here. Non-CEF related discussion goes in General Discussion!

Best way to pass data to javascript

Postby DJackson » Thu Jun 23, 2016 6:36 pm

I'm trying initiate a download upon a button press and then send the downloaded html to a javascript function to be displayed in my window. However, I can't seem to get the html to the javascript.

The native function is being called from the button press (name=="login").
The request is being initialized.
The request callback is being called.
The data (html) is being downloaded.
The javascript function (changeContent) is being called.
No method I have tried thus far to get the html to that function to be displayed has worked.
The alert box which should be displaying some content is displaying "undefined".

In my V8Handler:
Code: Select all
         else if (name == "login")
         {
            CefRefPtr<CefRequest> request = CefRequest::Create();

            // Set the request URL.
            request->SetURL("somesite.com/'somescript.php");

            // Set the request method. Supported methods include GET, POST, HEAD, DELETE and PUT.
            request->SetMethod("POST");

            const std::string& upload_data = "";
            CefRefPtr<CefPostData> postData = CefPostData::Create();
            CefRefPtr<CefPostDataElement> element = CefPostDataElement::Create();
            element->SetToBytes(upload_data.size(), upload_data.c_str());
            postData->AddElement(element);
            request->SetPostData(postData);

            // Create the client instance.
            CefRefPtr<MyRequestClient> client = new MyRequestClient(0, app_, object);

            // Start the request. MyRequestClient callbacks will be executed asynchronously.
            CefRefPtr<CefURLRequest> url_request = CefURLRequest::Create(request, client.get(), NULL);
         }


In my request client:
Code: Select all
   class MyRequestClient : public CefURLRequestClient {
   public:
      MyRequestClient(int request_type, CefRefPtr<Application> app = NULL, CefRefPtr<CefV8Value> object = NULL)
         : upload_total_(0),
         download_total_(0),
         app_(app),
         request_type_(request_type),
         object_(object){}

      virtual void OnRequestComplete(CefRefPtr<CefURLRequest> request) OVERRIDE {
         CefURLRequest::Status status = request->GetRequestStatus();
         CefURLRequest::ErrorCode error_code = request->GetRequestError();
         CefRefPtr<CefResponse> response = request->GetResponse();

         //Login request
         if (request_type_ == 0)
         {
            //download_data_ IS populating with html from the request
            object_->SetValue("newHtml", CefV8Value::CreateString(download_data_), V8_PROPERTY_ATTRIBUTE_NONE);
            //changeContent IS being called
            app_->browser_->GetMainFrame()->ExecuteJavaScript("changeContent();", app_->browser_->GetMainFrame()->GetURL(), 0);
         }
      }


The relevant javascript:
Code: Select all
    $(".loginBtn").click(function (e) {
        e.preventDefault();
        $("#loginPlaceholder").html("<img src=\"img/loading.svg\" style=\"width: 42px;\"></img>");
        window.login();
    });

function changeContent()
{
   alert(window.newHtml);
}
DJackson
Techie
 
Posts: 11
Joined: Fri Jan 22, 2016 3:48 pm

Re: Best way to pass data to javascript

Postby amaitland » Thu Jun 23, 2016 8:57 pm

If you'd provided the full example it probably would have been helpful.

Which object is your CefV8Context? To change an object outside of OnContextCreated you need to Enter and Exit a context.

apidocs3/projects/(default)/CefV8Context.html#Enter()
Maintainer of the CefSharp project.
amaitland
Master
 
Posts: 874
Joined: Wed Jan 14, 2015 2:35 am

Re: Best way to pass data to javascript

Postby DJackson » Thu Jun 23, 2016 9:50 pm

That solved my problem. Thanks!
DJackson
Techie
 
Posts: 11
Joined: Fri Jan 22, 2016 3:48 pm


Return to CEF Discussion

Who is online

Users browsing this forum: No registered users and 3 guests

cron