0

I have to use a XMLHttpRequest, but I don't know which data format is expected by the function request.send(). I searched for too long now.

I tried to pass a JSON object but it does not work:

    var request = new XMLHttpRequest();

    request.open("GET","fileApi");

    var data = {
        action: "read",
        targetFile: "testFile"
    };

       request.addEventListener('load', function() {
          if (request.status >= 200 && request.status < 300) {
             $("#messageOutput").html(request.responseText);
          } else {
             console.warn(request.statusText, request.responseText);
          }
       });
    request.send(data);

I get updateFile:155 XHR finished loading: GET "http://localhost/cut/public/fileApi".

But no data is received on the server. I made this simple check to approve this:

PHP (server side):

$action = filter_input(INPUT_GET, "action");
$targetFile = filter_input(INPUT_GET, "targetFile");

echo ("action = '$action' | targetFile = '$targetFile'");
exit();

Returns: action = '' | targetFile = ''

Unfortunatelly I can't use jQuery in my application, since the target is a C# Webbrowser (Internet Explorer) and it detects errors in the jQuery file and stops my scripts from working...

Black
  • 18,150
  • 39
  • 158
  • 271
  • [`send`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send) takes either a string or a `FormData` object, or binary data since more recent versions. – Bergi Aug 04 '17 at 14:31
  • 1
    "*a C# Webbrowser (Internet Explorer)*" - wat? jQuery should work in IE, try an older version if necessary. – Bergi Aug 04 '17 at 14:33
  • That looks like PHP on the server side, code that you can control. What data format do you *want* to use there? – Bergi Aug 04 '17 at 14:35
  • @Bergi, but It doesnt. A warning window appears. See my other post: https://stackoverflow.com/questions/45507858/c-sharp-windows-forms-app-send-request-by-opening-html?noredirect=1 – Black Aug 04 '17 at 14:35
  • You're not sending a JSON object, you're sending an object and that won't work. If you want to send a JSON string, use `JSON.stringify`. – Bergi Aug 04 '17 at 14:35
  • @Bergi, Thanks. I tried JSON.stringify, but how can I access this data string now in my PHP? e.g. `$data= filter_input(INPUT_GET, "data");` – Black Aug 04 '17 at 14:39
  • a valid json object should have its keys quoted. try that if it helps? – MikeChav Aug 04 '17 at 14:43
  • @Black https://stackoverflow.com/questions/7047870/issue-reading-http-request-body-from-a-json-post-in-php? – Bergi Aug 04 '17 at 14:46
  • @MikeChav, I tried it, but `var_dump($_GET);` returns `array(0) { }` – Black Aug 04 '17 at 14:48

1 Answers1

2

I don't know which data format is expected by the function request.send()

It can take a variety of formats. A string or a FormData object is most common. It will, in part, depend on what the server is expecting.

I tried to pass a JSON object

That's a JavaScript object, not a JSON object.

request.open("GET","fileApi");

You are making a GET request. GET requests should not have a request body, so you shouldn't pass any data to send() at all.

GET requests expect data to be encoded in the query string of the URL.

var data = {
  action: "read",
  targetFile: "testFile"
};

var searchParams = new URLSearchParams();
Object.keys(data).forEach((key) => searchParams.set(key, data[key]));
var url = "fileApi?" + searchParams;
console.log(url);
// and then… 
// request.open("GET", url);
// request.send();

Warning: URLSearchParams is new and has limited browser support. Finding a library to generate a query string is left as a (simple) exercise to any reader who wants compatibility with older browsers.

Black
  • 18,150
  • 39
  • 158
  • 271
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • According to this page (https://www.w3schools.com/js/js_json_objects.asp) it is a JSON object. I am just missing the quotes on the keys, does this make it not a json object? – Black Aug 04 '17 at 14:49
  • 1
    @Black — W3Schools is awful. That page is wrong. The first example is correct, but then it stops showing JSON and starts showing JavaScript. If you have JSON data in a JavaScript program then it will be a JavaScript *string*. Don't trust W3Schools. – Quentin Aug 04 '17 at 14:51