Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

XMLHttpRequest() or GM_XMLHttpRequest help


elmiguel's Avatar
Member
2,795 1

OK I am having issues with both these functions and cannot seem to know why it is not working.

XMLHttpRequest():

 req.open('GET', 'https://developer.mozilla.org/En/Using_XMLHttpRequest', false);   
 req.send(null);  
 if(req.status == 200){  
   alert(req.responseText);
 } ```


GM_XMLHttpRequest():
```markupGM_xmlhttpRequest({
	method: "POST",
	url: "http://www.site.com",
	headers: {"Content-Type": "WHAT IS THE MIME TYPE TO POST JUST TO A URL NOT A FORM!"},
	data: "key" + encodeURI('stringToEncode')
  }
});```

I have been messing around with both GET and POST methods with both of functions (not in the same code of course). For some reason I get back null or nothing at all, and others I get the error feedback I put in. Does anyone have a working code for me to review and see what I am doing wrong?. I have looked over a lot of material on the web and all seems to be correct, but I can not perform this simple action. 

Thanks in advance.

-elmiguel

elmiguel's Avatar
Member
2,795 1

Figured it out, it was my incorrect use of the headers and MIME Type

ex:

GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://www.google.com',
    headers: {
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Accept': 'application/xhtml+xml',
    },
    onload: function(responseDetails) {
        alert('Request for Google feed returned ' + responseDetails.status +
              ' ' + responseDetails.statusText + '\n\n' +
              'Feed data:\n' + responseDetails.responseText);
    }
}); 

elmiguel's Avatar
Member
2,795 1

OK, maybe not, works on google but when I try to request a page here it doesn't work:

var elements = ''; // whether null, blank or empty data is not being stored here.
GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://www.hellboundhackers.org/challenges/timed/timed8/index.php',
    headers: {
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Accept': 'text/plain',
    },
    onload: function(responseDetails) {
       elements = (responseDetails.responseText);// gets the data.
    }
});
alert (elements); // if tried to store the data is lost.

[EDIT]: Changed MIME Type to: text/plain. It does get the data, but for some reason it will not store the data into a variable


SySTeM's Avatar
-=[TheOutlaw]=-
20 0

How does GM_xmlhttpRequest handle cookies?


spyware's Avatar
Banned
0 0

You need to send your HBH cookies with the request.


elmiguel's Avatar
Member
2,795 1
function post(url, data) {
      GM_xmlhttpRequest({
        method: "POST",
        url: url,
        headers:{'Content-type':'application/x-www-form-urlencoded',        
                 'Cookie' : 'PHPSESSID=<DATA>;fusion_user=<DATA>'},
        data: "ans=" +encodeURI(data),
      });
    }
post('http://www.hellboundhackers.org/challenges/timed/timed8/index.php', a});


I am sending the Cookies but its not working


ghost's Avatar
0 0

If it doesn't automatically add it, you are going to have to add "Content-Length:" to that header.