3

I have to make a function that have to make a get into a public url, it will return me a plain text wich.

Depending on that i will make a validation.

I try to make it with the dojo ajax get method but since is a different domain it will not work ( only on Explorer).

How I can make that call to the external domain and manage the answer.

the url will be something like this.

http://my.socket:8080/?option1=1&option2=2

It can be with ajax or just a java class.

Jashwant
  • 26,663
  • 15
  • 65
  • 99
user1725253
  • 295
  • 1
  • 3
  • 12

2 Answers2

2

ajax is working on client side which means you are not able to make foreign domain ajax calls due to http://en.wikipedia.org/wiki/Same_origin_policy

it will be good technique if you write that GET call in your applications server side and make ajax call to your application which will act as gateway and fetch data from remote server

MySqlError
  • 600
  • 7
  • 19
  • Sounds goods, any Idea of how I manage the text that i Recieve from the url in my server side. I can make something like this to ho hit the external url , but how i manage the asnwer. URL url = new URL("http://socket."); URLConnection conn = url.openConnection(); conn.setDoOutput(true); BufferedReader in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) ); in.close(); – user1725253 Oct 06 '12 at 15:01
  • basically you need to write code which will be executed when you navigate to yourdomain.com/gatewaycode/maybemoreparams and will print data returned by foreign domain if you'll manage that then you'll make ajax call to yourdomain.com/gatewaycode/maybemoreparams am i missing something from your question ? you're using java on your server side ? – MySqlError Oct 06 '12 at 15:05
  • yes the application is in java, I like the idea that make the call in my server side and then with ajax call my same domain, What I,m not sure is how to make the call from my server side. – user1725253 Oct 06 '12 at 15:08
  • refer to http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java – MySqlError Oct 06 '12 at 15:09
0

Due to same origin policy, you cannot do simple ajax calls across cross domains.

You can ajax call your own server page which resides on same domain and that page can do HTTP get and get the content for you.

or you can use YQL

var url = 'http://my.socket:8080/?option1=1&option2=2'; // website you want to scrape
var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + url + '"') + '&format=json&callback=?';  
$.getJSON(yql,function(data){  
    if(data.query.results){
        var result = data.query.results.double.content.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
        alert(result);
    }
});

Reference

Community
  • 1
  • 1
Jashwant
  • 26,663
  • 15
  • 65
  • 99
  • I dont know about plain text ( you can filter at your end though ) but you can get xml. Just change this line `select * from html....` to `select * from xml....` – Jashwant Oct 06 '12 at 15:08