4

I have a custom website which checks on document.ready if there is a parameter called 'temp' in the URL-line. If so, I call a PHP-function on my server which stores the value in a database. Now the strange thing for me is that if manually type this:

http://mywebsite.org/?temp=7

in my browser, everything works fine. But if I use this website to test GET:

http://requestmaker.com/

my website does nothing.

This is my jQuery code, which checks the existence of a parameter:

$(document).ready(function() {
      var data = gup('temp', location.href);
      if (data != undefined) {
        $.ajax({
          data: {
            action: 'insertTemp',
            value: data
          },
          type: "GET",
          url: "SQL.php",
          success: function(data) {
            //alert("Data Saved " + data);
          },
          error: function(xhr) {
            alert(xhr.responseText);
          }
        });
      }

Do you think that the document.ready could be a problem because it is not checked at automated HTML-requests?

Edit:

Gup is a function I copied from the internet, it just filters for the parameter in the URL:

function gup(name, url) {
  if (!url) url = window.location.href;
  name = name.replace(/[\[\]]/g, "\\$&");
  var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)", "i"),
    results = regex.exec(url);
  if (!results) return null;
  if (!results[2]) return '';
  return decodeURIComponent(results[2].replace(/\+/g, " "));
}

It is declared outside the document.ready() clause.

This is the response code I get from requestmaker.com:

http://pastebin.com/WL2CQP4v

Yass
  • 2,658
  • 3
  • 13
  • 21
binaryBigInt
  • 1,526
  • 2
  • 18
  • 44
  • Have you looked in your browser's console for errors? Where are you defining `gup()`? – Jay Blanchard May 24 '16 at 16:53
  • Hi and thanks for your comment, no, my browser does not throw anny erros, gup is just a function i copied from the internet which filters the url for parameters: `function gup(name, url) { if (!url) url = window.location.href; name = name.replace(/[\[\]]/g, "\\$&"); var regex = new RegExp("[?&]" + name + "(=([^]*)|&|#|$)", "i"), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\+/g, " ")); }` – binaryBigInt May 24 '16 at 17:08
  • What about the request / response in the network tab of your browser's developer tools? – Jay Blanchard May 24 '16 at 17:24
  • This is the request/response if i use this command: `www.mywebsite.org/temp=2` http://pastebin.com/ZMG58Dc5 – binaryBigInt May 24 '16 at 17:48
  • Those are the headers, what about the actual data? – Jay Blanchard May 24 '16 at 17:56
  • This is in the `Query String Parameters` field, it contains the data sent to the PHP-Function: `action:insertTemp value:2` – binaryBigInt May 24 '16 at 18:06
  • No response from the PHP? – Jay Blanchard May 24 '16 at 18:07
  • Okay, this is the `echo` my PHP-function sends if the database `INSERT` was sucessfully: `Entered value successfully` (under the `Response` tab). So i guess that works fine. But i don't understand what's the difference between my manual insert and the `HTML GET`? – binaryBigInt May 24 '16 at 18:10
  • 1
    So, the AJAX is working. `console.log(data)` in your success function. – Jay Blanchard May 24 '16 at 18:12
  • `Entered value successfully` as well. – binaryBigInt May 24 '16 at 18:18
  • Now you need to check your web server's error logs. The AJAX is fine, but something is hinky with your PHP script. Maybe post your PHP code here too. – Jay Blanchard May 24 '16 at 18:25
  • Why should there be an error with my PHP-script? It returns the `echo` it should? Hm, i have to find out first how i can read my web server's error logs... – binaryBigInt May 24 '16 at 18:26
  • Because the AJAX works, yet nothing gets inserted to your database which is the responsibility of your PHP code. – Jay Blanchard May 24 '16 at 18:27
  • Oh, okay. Sorry it looks like you missunderstood something. The process and data i just described happens when i insert the URL `manually` in my browser. This is the data if i try to make a html request with requestmaker.com: http://pastebin.com/WL2CQP4v `Response Body:` is just the pure HTML/JS from my website. – binaryBigInt May 24 '16 at 18:43
  • If you're making a request with AJAX you can do it as a GET request. – Jay Blanchard May 24 '16 at 20:13
  • Is it possible to create ajax requests from a desktop app to a website? If i want to stick with HTML guess i have to send the request directly to the SQL function as @Reto mentioned? – binaryBigInt May 24 '16 at 20:28

2 Answers2

1

This is because requestmaker.com is only sending the request and showing you the response. Its not really executing the webpage in a web engine/browser.

You didn't write what your end-goal is, but if its for automated testing and like maybe you should check out travis-ci, phantomjs or similar tools.

Sarim
  • 3,124
  • 2
  • 20
  • 20
  • Thanks for your answer. My goal is to send data to the webpage and it stores it in a database automatically. I also tried to send a HTML-GET to my website from a desktop application with the same result that my website didn't store the data and i don't know why. I think `requestmaker` just does the same thing as my desktop app - it sends a html request. Of course that is not executed in a browser because usually it doesn't need a browser to send data via a html request to a website (this would actually take a lot of resources), but it should still work. – binaryBigInt May 24 '16 at 19:24
  • Generally speaking, there is two parts of a website, client-side and server-side. What you're doing (Checking url parameter, sending ajax request) is from client side. But the environments you're testing doesn't execute the client side. From what i understand your goal: You should write all the code in client side. Make a index.php that reads $_GET['temp'] and redirects to your target (SQL.php). Ex: `header('Location: /SQL.php?action=insertTemp&value='.$_GET['temp']);` NB: this is really bad code. You proper user input sanitation in prod. – Sarim May 24 '16 at 21:57
1

The problem is that your embeded ECMA (Javascript) code relies on a browser to be executed. The 'document.ready' wont be fired without a browser..

Reto
  • 1,305
  • 1
  • 18
  • 32