0
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.
    };
}

Everything else in the javascript file works, my problem is with the request to the server.. I'm trying to post a POST request to localhost with a script running in my browser (userscript injects this page (Also from //localhost/), so I know that much works.) The connection is a modified snippet I found somewhere and altered to my information.

The javascript console in chrome is returning no other errors, and my PHP file doesn't have an errors.. below : 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);
    }
}

The php doesn't run when the request with AJAX is sent, the text file isn't updated, and when I use $_GET with .php?songname=test it works..

Network returns 304, and 200 used from cache, and I'm still fairly new, I don't know how else to see if the connection is being made or not.

I'm not sure what I'm missing, but I've been working on this entire thing for about 12 hours, so any and all help is much appreciated..

==== Update ====

I managed to get the response working(-ish).. I get "XMLHttpRequest cannot load http://localhost/stream/musicgrabber.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '//www.domainhere.com' is therefore not allowed access." any way to bypass this somehow? It's sending information over localhost, so don't know what to do to fix it..

2 Answers2

0

why not try jquery post method

function sendSong(songN){
   $.post(
     url: "http://127.0.0.1/stream/musicgrabber.php",
     {data: "songname="+songN},
     function(response){
       alert(response);
     });
}
lanxan
  • 1
  • Trying to, still can't get jquery to inject... I got mine to work, but any thing more, or adding more scripts doesn't seem to work for me.. – vG Rejected Jun 25 '14 at 02:56
  • Dose access.log have the request record "post musicgrabber.php"? – lanxan Jun 25 '14 at 03:04
  • It does not, it's returning unexpected token, and somehow keeps messing up my page that I put jquery to be injected before my script. – vG Rejected Jun 25 '14 at 03:10
  • change this {data: "songname="+songN}, to {songname : songN}, – lanxan Jun 25 '14 at 03:17
  • Sorry about double posting.. jQuery causes scripts on the webpage to break and thus overflow my console with errors.. got about 1,000 errors in 5 seconds. I think I will have to stick with regular javascript.. – vG Rejected Jun 25 '14 at 03:17
  • well, it seems big trouble. I wish you can fix it. ^^ – lanxan Jun 25 '14 at 03:18
  • check this answer http://stackoverflow.com/questions/10143093/origin-is-not-allowed-by-access-control-allow-origin – Jeff Voss Jun 25 '14 at 04:13
  • Thank you, I can't post my answer for the entire page for 8 hours, but that part is fixed, thank you very much. – vG Rejected Jun 25 '14 at 04:33
0

Found the solution to my problem.. all of my request protocols, or order of forming the request was out of whack.. here is the fixed part of my script.

function sendSong(songN) {
    console.log("sendSong ran");
    var tlh = new XMLHttpRequest(), url = "http://127.0.0.1/stream/musicgrabber.php";
    tlh.open("POST", url, true);
    tlh.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    tlh.send("songname=" + songN);
    console.log(tlh);

    tlh.onreadystatechange = function () {
        console.log(tlh);
        if (tlh.readyState == 4 && tlh.status == 200) {
            console.log(tlh.responseText);
        }
    };
}

For the cross-site problem, I fixed that with adding:

header("Access-Control-Allow-Origin: *");

to my musicgrabber.php. Some other minor things I need to work out, but the requests are going through, thank you everyone for the help you provided.