Post(Xmlhttprequest) javascript does not work

Asked

Viewed 92 times

0

I am developing an extension for Chrome, but the post part is not working. The information is not enough. will the problem be the extension.

chrome.extension.onMessage.addListener(function(request, sender) {
if (request.action === "getSource") {
    var message, data, xhr;

    message = document.querySelector("#message");
    data = request.source;

    message.innerText = "Enviando ao servidor...";

    var postUrl = 'webservice.php';

    xhr  = new XMLHttpRequest();
    xhr.open('POST', postUrl, true);
    xhr.onreadystatechange = function() {
        if(xhr.readyState === 4) {
            if (xhr.status === 200) {
                message.innerText = "Resposta do servidor: " + xhr.responseText;
            } else {
                message.innerText = "Err: " + xhr.status;
            }
        }
      };

    //Enviando dados como RAW
    xhr.send(request.source);
     }
   });


function onWindowLoad() {

 var message = document.querySelector('#message');

 chrome.tabs.executeScript(null, {
 file: "getPagesSource.js"
 }, function() {

    if (chrome.runtime.lastError) {
    message.innerText = 'There was an error injecting script : \n' +   chrome.runtime.lastError.message;
   }
   });

    }

    window.onload = onWindowLoad;

webservice.php

<?php

 $rt = $_POST['data'];
 $fp = fopen("bloco1.txt", "a");


 $escreve = fwrite($fp, '$rt');

 // Fecha o arquivo
 fclose($fp);

 ?>  
  • Good afternoon sam, I do not receive the information on the php page... it is as if the information was not arrived.

  • i’m trying to do that. https://answall.com/questions/50083/pega-c%C3%B3digo-fonte-da-p%C3%A1gina-com-extens%C3%A3o-do-google-Chrome

  • the system takes the entire source code from the page and sends it in post form to php page.

  • below" xhr.open('POST', postUrl, true);" ?

  • Wasn’t..... :(

  • Do a simple test. Exchange send for: xhr.send("data=teste");... now from what I see, PHP is not returning anything.

  • unfortunately it is the same way.

  • I think the problem is the extension, it does not compile php. if the post arrives it is written in a txt file. but it will not.

  • I saw that the intention is to write in a file, but I think it would be better to make sure through an echo if the "date" is coming. Go that the problem is in the function of writing the file and you are stuck thinking that the "date" is not coming.

  • The extension shows php as text (the whole source). so it does not show echo.

  • So what’s the use of this code: xhr.responseText? responseText is the return of PHP, but PHP at my point of view returns nothing, has no echo or other response code.

  • Sam, look at all the code here. https://answall.com/questions/328330/problema-com-extens%C3%a3o-para-o-google-Chrome I just changed the php webservice

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.