How to execute ajax cross domain in jQuery using dataType 'text'?

In my javacript function, I call it ajax. It works fine, but only when accessing a web page from a firebird server. I have the same code on my testing server. Ajax asks to download some files, but only the Firebird server has its ip-registers with our clients in order to be able to scp there. I need to do the same if I access php files from a test server. All servers are on the intranet.

  • can i use dataType text for this?
  • Do I need to make any changes on the server side?

ajax call:

 url = "https://firebird"+path+"/tools.php?"; jQuery.ajax({ type: 'get', dataType: 'text', url: url, data: {database: database_name, what: 'download', files: files, t: Math.random() }, success: function(data, textStatus){ document.getElementById("downloading").innerHTML+=data; } }); 

Update 1

My small web application restores databases, so I can test them. Now I want to improve it so that I can connect to our customers and download a specific backup. Our client allowed only the firebird server firebird connect to its networks. But I have my own server dedicated to testing . Therefore, every time I want to load a database, I need to connect firebird . The source of my web application and the folder with all the backups are mounted in the same place on both firebird and testing servers. Right now my solution (for download) is working, but only from firebird. I work mainly with testing server only.

Update 2

I make two ajax calls. One of them is a pure jQuery call (I assume I can apply any solution to this), and the other is an ajax call from jsTree. I created a new question for this. It seems to me that I have to be suitable for the @zzzz option b).

+4
source share
4 answers

You have the following options:

a) You use the jsonp type as the data type, but this involves making changes on the server side to transfer the data back as json, not as txt .. this change can be as simple as

 { "text":<your current text json encoded> } 

and on the js side you use this as response.text; Having said that if you get a text file for your file from another domain, I'm not sure how easy it is for you to change the code.

b) Another option is that you write a handler / endpoint on your server, that is, in your domain that will make an HTTP request to this third domain, it receives the file, and you send the file back to your client and in fact now your client is talking only with your domain, and you control everything. like most Ruby-based yoyr questions, here is an example:

 req = Net::HTTP.get_response(URI.parse('http://www.domain.com/coupons.txt')) @play = req.body 

You can find more information about the same here.

Hope this helps.

+1
source

To do cross-domain queries, your options are pretty limited. As @Mrchief mentioned, you can make a server proxy and jsonp .

Another option is Resource Sharing (CORS) , a W3C working draft. Quote from this blog post :

The basic idea of ​​CORS is to use custom HTTP headers for the browser and server, it is enough to know each other to determine whether the request or response will be successful or unsuccessful.

For a simple request that uses GET or POST without custom headers and whose body is text / regular, the request is sent with an additional header called Origin. The Origin header contains the beginning (protocol, domain name, and port) of the requesting page, so that the server can easily determine if it should respond to a response.

You can find some live examples on this site .

You will need to make changes on the server side to accept CORS requests. Since you have control over the server, this should not be a problem. Another drawback with CORS is that it may not be compatible with older browsers. So, if some of your main audiences use incompatible browsers, a server-side proxy might be your best bet.

+3
source

I just want to offer an alternative.

I'm not too sure about the configuration of your network, but if you have access to DNS, perhaps it would be easiest if you just provided your servers with some arbitrary subdomain of the same domain. Something like www.foo.com for a website and firebird.private.foo.com for a Firebird server. Thus, it becomes a cross subdomain instead of a cross domain. Then somewhere in your JavaScript on both pages,

 document.domain = "foo.com"; 

This gentleman has reached this decision here .

+2
source

Another idea is to use your web server as a proxy. You will need to consider the security implications for this route.

0
source

All Articles