Search Unity

UnityWebRequest error

Discussion in 'Web' started by bdovaz, Dec 29, 2015.

  1. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,050
    I'm using a web service API from a social network (so I do not control the server) and I have the following error using this code:

     
  2. alexsuvorov

    alexsuvorov

    Unity Technologies

    Joined:
    Nov 15, 2015
    Posts:
    327
    Hello N3uRo
    I suppose you are trying to perform a cross-domain request, which is restricted by the browser.
    You should either modify CORS on the server side or host the main page on the same domain with the requested resources. You can check for additional information here http://docs.unity3d.com/Manual/webgl-networking.html
     
  3. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,050
    But I'm doing a request to an API that it's not mine (social network API). I can't change any "server" configuration.
     
  4. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,050
    It has to be a "UnityWebRequest" problem because if I put this (using jQuery) on the "index.html" it works:

    Note that I have to put "&callback=?" to use JSONP.

    I need a Unity staff answer before I start making a plugin that does this bridge (doing request and sending to a C# script).
     
  5. alexsuvorov

    alexsuvorov

    Unity Technologies

    Joined:
    Nov 15, 2015
    Posts:
    327
    Those JSONP requests are performed by jQuery differently. The provided callback makes it possible to overcome the cross-domain policy as the response is returned as JavaScript in the following form: CallbackFunction({JsonData})
    The browser cross-domain policy is not applied to scripts so the returned script can be successfully loaded, appended to the document and executed, launching your callback function with the returned JsonData as a parameter.

    It is not possible to perform this trick using UnityWebRequest, however, yes, you can implement a JavaScript function to perform this kind of requests and use it via jslib mechanism (http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html)

    Additionally, you can use the browser log to double check if your server actually returns the "Access-Control-Allow-Origin": "*" header. If it does not, then you need a plugin.
     
    Last edited: Dec 30, 2015
  6. alexsuvorov

    alexsuvorov

    Unity Technologies

    Joined:
    Nov 15, 2015
    Posts:
    327
    Alternatively, you can setup an intermediate proxy server, which will proxy your API request and send the result back to your page adding proper CORS (not required if proxy and WebGL content share the same domain). In this case you will only have to modify the request url, while the game code remains unchanged.
     
    Last edited: Dec 31, 2015
  7. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,050
    Thanks, I have implemented the two solutions because the first one doesn't require a server (but only can manage GET requests) while the seconds one requires it (but can manage GET/POST/DELETE...).

    It's for an Asset Store plugin.