Code Monkey home page Code Monkey logo

Comments (10)

alexandru avatar alexandru commented on May 24, 2024

From your sample, you're trying to do a cross-domain request to a server that probably doesn't allow it. You can't pick an arbitrary domain from the Internet and do requests to it, because that server must also serve some headers back (read my article on the subject).

Another problem may be due to the limitations of IExplorer. If you'll look in the Troubleshooting document you'll see that IExplorer has problems with any Content-Type sent, other than text/plain.

This sucks, because normally sending data such as "www-urlencoded" will not work. So you need to send it as plain/text. But this also means that your server-side web framework may not parse it properly.

So do this test ... make a request and inspect the RAW data that was received by the server. For instance in PHP you need to read $HTTP_RAW_POST_DATA. Every web framework has a similar mechanism. You may need to parse it (manually decode it before usage, and not rely on the request object).

It's a PITA, but you can workaround it. If the above happens, blame Microsoft, not me :-)

from crossdomain-requests-js.

RavBhagdev avatar RavBhagdev commented on May 24, 2024

Thanks for reply. I am actually using Firefox 11.

I have made sure that I have http://localhost:9094/crossdomain.xml is accessible (which is my remote server I am requesting from). So it should be allowing the requests?

When I try with chrome, it doesn't work at all!

from crossdomain-requests-js.

RavBhagdev avatar RavBhagdev commented on May 24, 2024

And this XML looks like this:

Am I doing something wrong?

from crossdomain-requests-js.

alexandru avatar alexandru commented on May 24, 2024

By using this module, Firefox 11 should not use the Flash fallback, meaning that the cross-domain policy file is irrelevant. And if it works in Firefox 11, then it must work in Chrome. Right now I have the latest versions of Chrome and Firefox, and this sample works just fine:

http://bionicspirit.com/projects/crossdomain-requests-js/

So besides that crossdomain policy file (which is used when the Flash fallback is needed on older browsers), your server must return on response the CORS headers, as described here:

https://github.com/alexandru/crossdomain-requests-js/wiki/Usage
http://bionicspirit.com/blog/2011/03/24/cross-domain-requests.html

You also haven't replied to my suggestion. Inspect the server-side RAW POST data that was sent to the server.

from crossdomain-requests-js.

RavBhagdev avatar RavBhagdev commented on May 24, 2024

I do apologise. I thought the cross browser was going to change tomcat headers. I am trying to find if there is a way to set these headers in Tomcat configuration for all requests. Do I have to do this with a servlet or on page explicitly? I will try that.

from crossdomain-requests-js.

RavBhagdev avatar RavBhagdev commented on May 24, 2024

I tried setting the headers inside the servlet I was calling like so:
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader("Access-Control-Allow-Origin", req.getHeader("origin"));
res.setHeader("Access-Control-Allow-Headers", "Content-Type, *");

But that didn't work. I am trying to access the parameters which will be before the server returns the response, so not sure how this really will work... Quite confused.

from crossdomain-requests-js.

alexandru avatar alexandru commented on May 24, 2024

When the browser makes the request, the server does receive it in all cases, regardless of those headers. The problem is that, in order for the browser to receive the response and do something useful with it, then it must receive those headers from the server, otherwise the ajax call ends in error. For setting those headers on all requests, you could use a Servlet Filter ... http://www.oracle.com/technetwork/java/filters-137243.html

So you need those headers, but probably not for the problem you specified originally, which is that you're not receiving the params POSTed.

I'm repeating myself here, on the server side inspect the RAW data posted. With the Servlet API, you can do it like this ...

   InputStream body = request.getInputStream();

Do this before anything else, and log that text somewhere, because that stream is read only once, and other methods such as getParameter are triggering it. So if you see anything in there that resembles the request you made, then it's a Content-Type problem. If you don't see anything in there, then the data was not sent.

Oh, and another thing, the "data" parameter only works for POSTs. If you want to do GETs, then append that text to the URL, after a question mark.

from crossdomain-requests-js.

RavBhagdev avatar RavBhagdev commented on May 24, 2024

Hi Alexandru,

Thanks for baring with me.

And YES! It is receiving the parameters as you described! I printed the body and it is: some=json&c=somedata&callback=somecallback

These are the three parameters I passed in my POST request via 'data'! So they are definitely being sent but when I do req.getParameter("some") or req.getAttribute("some") for example, I keep getting null! :(

from crossdomain-requests-js.

RavBhagdev avatar RavBhagdev commented on May 24, 2024

I appreciate your help with this. I am a bit new to this.

from crossdomain-requests-js.

RavBhagdev avatar RavBhagdev commented on May 24, 2024

I've managed to get it working! I am now reading inputstream on server side directly to get what I want from my POST request. No ideal but it works! Not sure why they are not accessible from the usual getAttribute method. Anyway, thanks a lot for your help!

from crossdomain-requests-js.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.