进行ajax调用以获取两个javascript文件到单个php文件的响应

时间:2017-09-16 14:22:28

标签: javascript php jquery html ajax

我正在使用JQuery Ajax。我要求响应的PHP文件在服务器上。第一个JavaScript文件与PHP在服务器上的同一目录中,第二个文件与我本地保存,我在我的chrome扩展中使用。我在PHP中创建了一个代码块,我从本地JavaScript文件运行Ajax并获得响应,我想要的是在服务器上调用JavaScript,并且可以通过两种方法解决。

1)使Ajax调用相同的PHP代码块,保存在服务器的同一目录中。

2)从本地JavaScript文件调用服务器上的JavaScript。当本地JavaScript获取响应时,请使用一种方法从本地JavaScript调用服务器上的JavaScript。

基本上我尝试了它们,我已经对同一块代码进行了第二次Ajax调用,但是当服务器html文件首先运行并首先加载JavaScript时,它不会从该Ajax请求返回任何内容。 在第二种方法中,我使用了chrome的postMessage API。但它阻止了从本地到服务器的交叉原始消息传递。并且域名冲突会停止传递我的消息来调用服务器JavaScript。

请求数据并获得响应的本地文件JavaScript

function sendUrl(url){
   console.log("Passed to sendURL : " + url);	
   $.ajax({
    method:"GET",
	url:"http://www.bitbaysolutions.com/connections.php?updateRow=true",
	data:{
		ajaxified : url
	},
	success:function(res){
          console.log("PHP RESPONSE " + res);
	}
   });
}

  

当我从这个Ajax调用得到响应时,我想调用服务器JavaScript。只是成功。

当我在同一个Ajax调用响应中使用post Message API时

function sendUrl(url){
   console.log("Passed to sendURL : " + url);	
   $.ajax({
    method:"GET",
	url:"http://www.bitbaysolutions.com/connections.php?updateRow=true",
	data:{
		ajaxified : url
	},
	success:function(res){
       console.log("PHP RESPONSE " + res);
         window.postMessage(res,"*");
	}
   });
}

并在服务器JavaScript文件上编写此代码以接收消息

var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent";
var eventer = window[eventMethod];
var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message";

eventer(messageEvent,function(e) {
   console.log(e.data);
});

  

运行后,我收到以下错误:GET chrome-extension://jifdgpnmlcmlcbpmemiecblbddpdejhe/Row%20Updated net::ERR_FILE_NOT_FOUND

当我在这个本地文件上得到响应时,我想调用服务器JavaScript文件。如果还有其他可能的解决方案,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

看起来您应该使用Chrome's Message传递代替window.postMessage

所以扩展程序中的Javascript看起来像这样:

function sendUrl(url){
   console.log("Passed to sendURL : " + url);	
   $.ajax({
    method:"GET",
	url:"http://www.bitbaysolutions.com/connections.php?updateRow=true",
	data:{
		ajaxified : url
	},
	success:function(res){
       console.log("PHP RESPONSE " + res);
       chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
           chrome.tabs.sendMessage(tabs[0].id, {thisCameFromTheServer: res}, 
               function(response) {
                   //If your script on the page sends a response, 
                   // deal with it here.
                   //console.log(response.farewell);
               }
           );
       });
	}
   });
}

接收消息的页面上的Javascript必须由扩展名插入Content Script,该脚本可以直接操作页面,也可以调用从服务器加载的JS。

内容脚本将收到如下消息:

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        
        if (request.thisCameFromTheServer) {
            console.log(request.thisCameFromTheServer);
        }
       /* I *think* that you have access to the whole page DOM, and any JS
       on the page, from here, so if there's a JS function you want to call
       that's on the page you probably can. */
    }
);