Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过Post跨站点HTTP_Javascript_Php_Jquery_Post_Jquery Post - Fatal编程技术网

Javascript 通过Post跨站点HTTP

Javascript 通过Post跨站点HTTP,javascript,php,jquery,post,jquery-post,Javascript,Php,Jquery,Post,Jquery Post,正在尝试将数据发送到其他域。我目前正在本地主机上工作 我现在尽量让它简单 js: Chrome在Developer Tools>Timeline中显示此请求,如下所示: Self Time: 0 Start Time: 6 ms Resource: localhost/web/x/boot/ajax/x.php? Request Method: POST Call Stack: send @ jquery.min.js:4 m.extend.ajax @

正在尝试将数据发送到其他域。我目前正在本地主机上工作

我现在尽量让它简单

js:

Chrome在
Developer Tools>Timeline
中显示此请求,如下所示:

   Self Time: 0
   Start Time: 6 ms
   Resource: localhost/web/x/boot/ajax/x.php?
   Request Method: POST
   Call Stack:
   send @ jquery.min.js:4
   m.extend.ajax @ jquery.min.js:4
   m.(anonymous function) @ jquery.min.js:4
   window.onbeforeunload @ x.js:249
以及我记录任何加载的最简单的php文件:

<?php
   //include 'config.php';

    $handle = fopen('temp.txt','w') or die('Cannot open file:  ');
        fwrite($handle, 'hit');
        fclose($handle);
    exit();
?>
返回一个错误:

 <blank>
 error
 error
 .

错误
错误
.

window.onbeforeunload:当窗口关闭时,您的请求可能在完成之前被浏览器终止。

要在本地主机中测试跨域,您可以使用192.168.0.1之类的ip地址发出请求

    window.onbeforeunload = function() {
        var url = "http://{ip-address-here}/web/x/boot/ajax/x.php?";
        $.post(url, { thing: "value" }, function(data) {});
    };

检查来自其他机器的相同呼叫。

收到了。不使用jQuery、post或ajax。返回javascript以发出HTTP请求。我不想在网页上加载数据。我实际上是在用户离开页面时发送数据

下面点击并创建我上面提到的文件

window.onbeforeunload = function() {
   //...  create params
    var XHR = new XMLHttpRequest();
    XHR.open('POST', "http://localhost/web/x/boot/ajax/x.php?", false);
    //XHR.open('POST', "http://127.0.0.1/web/x/boot/ajax/x.php?", false); // also works
    //XHR.open('POST', "http://{ip-address}/web/x/boot/ajax/x.php?", false); // also works
    XHR.send(params);
}
正在发送参数。现在就要想办法解决这个问题:p


无论如何谢谢你

普通的HTML标签?不,你不需要那个。可能在PHP文件中添加一个
echo
或其他内容,然后查看
success
函数的
$.post
.Ok,看起来好像
$.post(url,{},function(){alert('successful');})。错误(function(){alert('failed');})失败在我实现时被调用,但仍然没有在服务器上创建文件<代码>$.post(url,{},函数(){alert('successful');})。错误(函数(){alert('failed');})失败是在我实现thisDon't This时调用的,
$.post(url,{},function(){alert('successful');})。错误(function(){alert('failed');})失败
    window.onbeforeunload = function() {
        var url = "http://{ip-address-here}/web/x/boot/ajax/x.php?";
        $.post(url, { thing: "value" }, function(data) {});
    };
window.onbeforeunload = function() {
   //...  create params
    var XHR = new XMLHttpRequest();
    XHR.open('POST', "http://localhost/web/x/boot/ajax/x.php?", false);
    //XHR.open('POST', "http://127.0.0.1/web/x/boot/ajax/x.php?", false); // also works
    //XHR.open('POST', "http://{ip-address}/web/x/boot/ajax/x.php?", false); // also works
    XHR.send(params);
}