Question: I develop a JavaScript application which uses oauth2 authentication. Now I want to load/show an image from server which is behind this authentication mechanism. Therefor I send an xmlHttp-request to the rest-server and get the URI of the image as response.

After the request I try to append the URI to the src of an image and the application responds with 401.

How can I tell the browser to reuse my authentication for this image as well?

This is a part of the xmlHttp-request for getting the URI.

```
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);

xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
xhr.setRequestHeader('Authorization','bearer '+token);
xhr.send(null); 

xhr.onreadystatechange = function() {
    console.log(xhr);
    if (xhr.readyState == 4 && xhr.status == 200) {
       var img = document.createElement('img');
       img.src = xhr.responseURL;
       document.body.appendChild(img);
    }
}

```

Did I forgot something?

Answer: 
I think no need to declare $p and not set property and its value in $p, you can pass values of $getProductType object as mentioned below.

```
if ($product['product_type'] != null) {
   $getProductType = $this->ProductTypes->find()
     ->where(['title LIKE' => '%'.$product['product_type'].'%'])
     ->first();
   debug($product['product_type']);   // this is not empty
   debug($getProductType);            // this shows 'id' => 1 in array
   debug($getProductType->id);        // this shows 1
   if (!empty($getProductType)) {
      return $this->saveNewBulkProduct($product, $getProductType->category_id,  $getProductType->subcategory_id, $getProductType->id);
   }
}

```
you have to declare first `$p = new stdClass();` on line 11