Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

We are working on java project in which backend is java + spring and fronted is angular 2 + HTML.

I want to do cross domain html parsing but we don't have permission to access external links on server side as we have some security issues for outsite domains, so we have to get the DOM content of link on client side using jquery.

I have tried these:

var url = "http://xyz.aspx";

$http({
    method: 'JSONP',
    url: url,
    params: {
        format: 'jsonp',
        json_callback: 'JSON_CALLBACK'
    }
}).
success(function(response) {
    $scope.test = response;
}).
error(function(status) {
    //your code when fails
});

The external link which I need to parse contains many href links. I also need to parse content of those links.

Have tried above mentioned code:

getting error in console - Uncaught SyntaxError: Unexpected token < on xyz.aspx page

What will be best solution to get content of the pages and pass to server side for parsing?

share|improve this question

marked as duplicate by Quentin javascript 35 secs ago

This question was marked as an exact duplicate of an existing question.

    
Any feedback or sugggestions Geetanjali? – lin 42 mins ago
    
@lin No, Just now I have updated my question, please response if you found something on it. – Geetanjali 14 mins ago
    
Still the same answer. Add CORS or create a PROXY which supports CORS and tunnels your request to your endpoint. This is an open proxy but I do not recommend to use open proxys while all our data is going thrue this foreign service. crossorigin.me – lin 11 mins ago
    
Updated my answer. It does include 3 possible solutions now. That would be enough for this moment, isnt it? – lin 5 mins ago

getting error in console - Uncaught SyntaxError: Unexpected token < on xyz.aspx page

This means that the server callback isn't a valid JSON object. Please ensure your server returns a valid JSON object. In that way this error will go away. JSONP needs a valid JSON body response to make it work.


If your server side does not return a JSON Object try to use a GET request and enable CORS.

$http({
    method: 'GET',
    url: "http://xyz.aspx",
}).success(function(response) {
    $scope.test = response.data;
}).error(function(status) {
    //your code when fails
});

If you are still be unable to add CORS to your backend you could create an PROXY application yourself. This for example is an public CORS Proxy https://crossorigin.me/. Adding an example on how to create a proxy service would be to much. Please research by yourself. There are a lot of examples in the web.

share|improve this answer
    
I can't change server content i.e. http link, I need only content from that server link. – Geetanjali 1 hour ago
    
@Geetanjali Updated, no way to make it work without having CORS enabled. – lin 1 hour ago
    
"JSONP needs a valid JSON body response " — It doesn't. It needs a JavaScript application, not JSON. – Quentin 1 min ago

See ng.$http. Your URL is missing the callback parameter,instead of giving like

 json_callback: 'JSON_CALLBACK'

Try below way

$http({
    method: 'jsonp',
    url: 'http://xyz.aspx?callback=JSON_CALLBACK',
    params: { name: 'xyz' }
}).success(function(data, status , header, config) {
    console.log('success');
}).error(function(data, status , header, config) {
    console.log('error');
});
share|improve this answer
    
I have tried these also, but I got same error. – Geetanjali 12 mins ago

Not the answer you're looking for? Browse other questions tagged or ask your own question.