1

I want to fetch CSV using cross domain AJAX request. I am using jquery.

I know we can use JSONP to for cross domain ajax request, but here I have to fetch CSV not JSON.

Its a intranet site and require login, so I cannot use YQL.

Is there any other way. below is my code

        $.ajax({
        url: 'http://si-dtp-219:3333/WS/WS/dshbrdData?reportname=Svt',
        dataType : 'text',
        callback : callbackFunction,
        crossDomain : true,
        type: 'GET',
        timeout: 50000,
        success: function(res) {
            callbackFunction(JSON.stringify(res));
        },
        complete: callbackFunction,
         error: function(x, t, m) {
            if(t==="timeout") {
                alert("got timeout");
            } else {
                console.log(x);
                console.log(t);
                console.log(m);
            }
        }


    });
4
  • If res is just the contents of a csv file, what's the problem? Commented Aug 17, 2012 at 13:19
  • What server-side language are you using? Commented Aug 17, 2012 at 13:19
  • @drim :- Its a cross domain request so I don't need to bother about serverside language. I just need to hit the url using javascript and get the content. If I am copying the response content and save it to local and fire ajax request like url:'local.txt', its working very fine. Commented Aug 17, 2012 at 13:22
  • 1
    CSV is no valid subset of JavaScript, so the JSONP technology won't work unless you get the file in one JS string and are allowed to wrap it in a callback function. If that is not possible, you will need to use a Proxy. Commented Aug 17, 2012 at 13:27

2 Answers 2

1

For our intranet sites, we frequently use proxy scripts to handle this. That is, you write a script inside your intranet that makes the request on your behalf. If you're using PHP it could be as simple as:

<?php
echo file_get_contents("whatever_url");
?>

Then in your Javascript application you just make your AJAX call to this page inside your domain. That way there are no cross domain issues at all (the request from the client is to a page inside its domain, the server side request has no restrictions on its domain).

Sign up to request clarification or add additional context in comments.

4 Comments

I cannot use any server side scripting language.. I have to fetch data using AJAX..
I believe that means you are out of luck.
@AnupSingh This technically is still AJAX, you're just making a request to your own page instead of the original, and letting the proxy do the second part of the request. If you have no control over your own server side scripting, then this is more complicated, and I'm not sure there is a satisfying solution.
There are two ways to fetch cross-origin data using AJAX if you cannot use any server code at all. 1. JSONP, which relies on the data being in JSON format - CSV will not work! 2. The server where the data resides must support CORS (Cross-Origin Resource Sharing). If the server already supports CORS you are home and hosed. If not try asking the owners of the server if they can enable it.
0

Well, you can still use JSONPish call. What you should do is add a script tag with your URL that assigns the value a variable and than do what you want with that data. You should use pure JS instead of jQuery as it needs JSON format.

http://devlog.info/2010/03/10/cross-domain-ajax/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.