Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [om2m-dev] Cannot send HTTP request from html file

Hello Paul and all,

For security reasons, AJAX cross domain is not allowed by default.
Even if you are using localhost, OM2M server and your web server are using different ports and so considered as different domains.
Your browser will block any AJAX requests sent to a domain different from the one running your web application.

There are several alternatives to make cross domain requests work:

1) Update OM2M source to add "Access-Control-Allow-Origin: *" header in HTTP response to allow AJAX cross domain.
I did not tried this before, but it should work.

2) If you don't want to update the OM2M source code, you can start a proxy server (e.g. using nodejs or java) and configure it to forward any received requests to OM2M server.
The proxy server is a program (not a browser running _javascript_ code) so no issue for the proxy  program to reach the OM2M server.
You can use one of the following proxy:
- Java Socket proxy: http://www.jcgonzalez.com/java-simple-proxy-socket-server-examples
- Nodejs HTTP proxy: https://github.com/nodejitsu/node-http-proxy#setup-a-stand-alone-proxy-server-with-proxy-request-header-re-writing

3) Incorporate your web server in OM2M platform as an OSGi plugin and attach it to the Jetty Server.
The current OM2M resource tree web interface relies on this solution.

4) JSONP serialization enable to bypass cross domain issue however not supported by OM2M.

Hope this clarify the situation for you.

Regards,

-- 
Mahdi Ben Alaya, Ph.D
CEO, Sensinov

benalaya@xxxxxxxxxxxx
+33 671 939 045

Global IoT Platform
www.sensinov.com

Le 22/04/2017 à 18:54, Paul Guirbal a écrit :
Hello everyone,

My team and I are students at INSA Toulouse, working on a project using OM2M.
I am trying to send a simple HTTP GET request from an html file (as a user interface).
Here is the code snippet exported from postman that is supposed to send the request :

... (awesome) html content before ;) ...

<script type="text/_javascript_">

            function sendRequest() {
                $("#req_res").html("--");
                var data = "">
                var xhr = new XMLHttpRequest();
                xhr.withCredentials = true;

                xhr.addEventListener("readystatechange", function () {
                  if (this.readyState === 4) {
                    console.log(this.responseText);
                  }
                });

                xhr.open("GET", "http://127.0.0.1:8080/~/in-cse");
                xhr.setRequestHeader("x-m2m-origin", "admin:admin");
                xhr.setRequestHeader("accept", "application/xml");
                xhr.setRequestHeader("cache-control", "no-cache");
                xhr.setRequestHeader("postman-token", "3ae1710c-2bb0-2085-9e5e-204486399d93");

                xhr.send(data);
            }
</script>

... html ...

On postman, this request seems to work perfectly, but when I try to send it with XMLHttpRequest(), I get the following error in the web console :

XMLHttpRequest cannot load http://127.0.0.1:8080/~/in-cse. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 400.

I tried also with AJAX but could not find any solution either.
I am searching for a long time on the web trying to get rid of this but I could not find any help. I tried so many different things that I am a bit lost right now.
Does anyone know how to deal with this kind of issue?

Thank you in advance from the whole team!

Paul Guirbal


_______________________________________________
om2m-dev mailing list
om2m-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/om2m-dev


  


Back to the top