Chrome扩展(Js文件)到Php数据发送

时间:2015-09-03 04:32:13

标签: javascript php jquery ajax google-chrome-extension

所以我对Chrome扩展程序非常新,我想用第一个chrome扩展程序完成的工作就是将chrome扩展名发送到php文件。

我已经尝试了$.post方法和$.ajax但是php中的isset函数检查发布数据总是失败。

这是我的代码:

  • 的manifest.json

    {
      "manifest_version": 2,
    
      "name": "First Chrome Extension",
      "description": "Send Url On Click",
      "version": "1.0",
    
      "browser_action": {
       "default_icon": "icon.png",
       "default_popup": "popup.html"
      },
    
      "content_scripts": [
        {
        "matches": ["http://*/*"],
        "js": ["jquery-2.1.4.min.js"]
        }
      ],
    
      "permissions": [
      "activeTab",
      "http://*/", 
      "https://*/"
      ]
    }
    
  • POPUP.html

    <html>
      <head>
        <title>First Chrome Extension</title>
        <script src="popup.js"></script>
    
      </head>
      <body>
                <form name="testform">
                <input type='button' id='alertButton' value='Send URL'>
                <div id="returnfromphp"></div>
                </form>
                <script  src="jquery-2.1.4.min.js"></script>
      </body>
    </html>
    
  • POPUP.js

    document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('alertButton').addEventListener('click', ShowUrl);
    }); 
    
    
    function ShowUrl(){
        chrome.tabs.getSelected(null, function(tab) {
        myfunction(tab.url);
    });
    
    }
    
    function myfunction(tablink) {
        var urlretained = tablink;
        alert(urlretained);
    
        $.ajax({
                type: "POST",
                url: "myfile.php",
                data: {myData:urlretained},
                success: function(data){
                    alert('Items added');
                },
                error: function(e){
                    console.log(e.message);
                    alert('Addition Failed');
                }
        });
    
    }
    
  • myfile.php

    <?php
    if(isset($_POST['myData'])){
            echo"Data Detected";
    }else{
            echo"Data Not Set";
    }
    ?>
    

0 个答案:

没有答案