简体   繁体   中英

Cross-domain issue with Jquery

I am trying to create a chrome extension which will look up the meaning of input vocabulary from this URL: http://hanviet.org/ajax.php?query=%E6%97%A5&methode=normal

I made an ajax call by using jquery but got an error because of the cross-domain issue: "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access" . Ok, I guest that instead of make a request directly to the URL, I need to call it through a proxy pages as below:

$.get("/myproxy.php?query=日&methode=normal", function( data ) {
    alert( "Load was performed." );
});

After doing a google search, there is another chrome extension named DHC to makes http request: https://www.sprintapi.com/dhcs.html . and It works perfectly!

I am wondering that does DHC tool also send a request through its proxy or there is another way to make a direct request that I dont know.

Thank you!

    $.ajax({
        type: "GET",
        url: 'URL',
        jsonp: 'callback',
        dataType: 'jsonp',
        data: {},
        success: loginSuccess,
        crossDomain: true,
        error: ajaxFailed,
        contentType: 'application/json',
        async: false
    });

    function ajaxFailed(result) {
        alert("Failed: " + result.status + ' ' + result.statusText);
    }

    function loginSuccess(data) {
        alert('Result: ' + data.d);
    }

If you use the developers tools of chrome on that site, on Network tab you will see that after pressing the send button it loads the content from https://www.sprintapi.com/api/proxy , so yes, it should be using a proxy.

Even more, as you say the Access-Control-Allow-Origin would'nt let they did it on another way I think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM