2

I send the url to a php file in chrome extension, and need get response, but doesn't work.

manifest.json

{
  "name": "Get pages source",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Get pages source from a popup",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": ["http://localhost/"]
}

popup.js

chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
    var url = tabs[0].url;
var parametros = {
            "url" : url
    };
$.ajax({
    type: "POST",
    data: parametros,
    url: 'file.php',
    success: function(data) {
        var res = jQuery.parseJSON(data);
        alert("success");
    },
    error: function(e) {
        alert("error");
    }
});

});

file.php - Is in the same folder, in localhost

<?php 
$resultado = $_POST['url']; 
echo $resultado;
?>

and the popup.html

<html style=''>
<head>
    <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src='popup.js'></script>
</head>
<body style="width:400px;">
<span id="resultado">0</span>
</body>
</html>

This code only return a empty array.

Not is posible use Ajax in chrome extension?

EDIT:

I charge jquery with a local file, not CDN, and work fine.

Other issue is in my code, I thing that is in jQuery.parseJSON.

Thanks to all ;)

Phyron
  • 613
  • 1
  • 9
  • 25
  • Is the PHP-file located in the extension folder? – M. Eriksson Jan 13 '16 at 22:15
  • Yes Magnus, the file route is good. – Phyron Jan 13 '16 at 22:30
  • As far as I know, you can't run PHP in a chrome extension? – M. Eriksson Jan 13 '16 at 22:31
  • Is the Chrome extension a webserver? Does it have a way to parse and execute PHP? – Jay Blanchard Jan 13 '16 at 22:33
  • 1
    No, chrome extension can't execute php. But is posible with ajax send a request to a externar php file. the php file is in localhost, apache server. Example: http://stackoverflow.com/questions/24016609/how-to-make-a-post-request-using-ajax-in-a-chrome-extension – Phyron Jan 13 '16 at 22:58
  • It should work. Try using an permission. see where that gets you. if you get something that means you formatted your url pattern incorrectly. – Marc Guiselin Jan 14 '16 at 06:09
  • I try permission and: "tabs","http://*/*","https://*/*","file://*/*". Doesn't work :( No ajax alerts displayed, succes or error. And console is empty. – Phyron Jan 14 '16 at 10:52

1 Answers1

3

Your problem is with the URL you're trying to POST to.

$.ajax({
    type: "POST",
    data: parametros,
    url: 'file.php', // That's a relative URL!
    success: function(data) {
        var res = jQuery.parseJSON(data);
        alert("success");
    },
    error: function(e) {
        alert("error");
    }
});

Assuming your extension's ID is aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, then your popup will have the URL chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/popup.html, and this will result in a request to chrome-extension://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/file.php, not your local server that you said you're using in the comments.

You need to use the full URL:

    url: 'http://localhost/file.php'
Xan
  • 74,770
  • 16
  • 179
  • 206
  • BTW, don't forget to debug your popup, see [how](https://developer.chrome.com/extensions/tut_debugging). – Xan Jan 14 '16 at 14:05