ajax localhsot,来自localhost的ajax请求没有响应

博主在尝试使用JavaScript的AJAX进行POST请求到localhost的PHP脚本时遇到问题。请求似乎没有触发PHP的响应,PHP文件中的功能未执行且文本文件未更新。尝试通过用户脚本注入页面并在Chrome控制台中运行,但遇到了'Access-Control-Allow-Origin'错误。尽管使用GET方法可以正常工作,但POST请求导致跨域问题。
摘要由CSDN通过智能技术生成

function sendSong(songN) {

console.log("sendSong ran");

var tlh = new XMLHttpRequest(), url = "http://localhost/stream/musicgrabber.php";

tlh.open("POST", url, true);

tlh.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

console.log(tlh);

//output=XMLHttpRequest {statusText: "", status: 0, response: "", responseType: "", responseXML: null…}

tlh.onreadystatechange = function() { //readystate never changes.

console.log(tlh); //this doesn't fire

if(tlh.readyState == 4 && tlh.status == 200) {

var return_data = tlh.responseText;

}

tlh.send("songname="+songN); //can't tell if this is sending or not.. nothing my php file is supposed to do, happens.

};

}

javascript文件中的其他所有内容都有效,我的问题在于对服务器的请求。我试图将POST请求发布到localhost,并在我的浏览器中运行脚本(usercript注入此页面(同样来自// localhost /),所以我知道这很有效。)连接是我在某处找到的修改后的片段,并根据我的信息进行了修改。

chrome中的javascript控制台没有返回任何其他错误,我的PHP文件没有错误..下面:musicgrabber.php

$ssn = file_get_contents("http://localhost/stream/currentsong.txt");

if($ssn != $_POST['songname']) {

saveSong($_POST['songname']);

}else{

echo "You messed up, or it's the same song";

}

function saveSong ($sname){

$fo = fopen("currentsong.txt", 'w');

fwrite($fo, $sname);

fclose($fo);

$fcheck = file_get_contents("songhistory.txt");

if($fcheck){

$hday = date('\[m\/d\/ g:i a\ \] \\n');

$writeto = $sname + $hday + $fcheck;

file_put_contents($writeto);

}

}

当发送带有AJAX的请求时,php不会运行,文本文件没有更新,当我使用带有.php的$ _GET时?songname = test它有效..

网络返回从缓存中使用的304和200,我仍然相当新,我不知道如何查看是否正在建立连接。

我不确定自己错过了什么,但我已经在这整个事情上工作了大约12个小时,所以非常感谢所有人的帮助..

====更新====

我设法让响应工作(-ish)..我得到" XMLHttpRequest无法加载http:// localhost /stream/musicgrabber.php。 No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' // www.domainhere.com'因此不允许访问。"以某种方式绕过这个?它通过localhost发送信息,所以不知道如何解决它..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值