简体   繁体   English

带有jQuery的Ajax跨域请求

[英]Ajax crossdomain request with jquery

I trying to make a call to an external domain using $.ajax() and it WORKS, the server receives the call, but the response in firebug errors out in jquery.js line 7760. I've been beating my head at this all day and don't feel like I've made it much further. 我试图使用$ .ajax()对其进行外部调用,并且工作正常,服务器接收到该调用,但是在jquery.js第7760行中出现了萤火虫错误中的响应。一天,不要觉得我已经走得更远了。

$.ajax({
            type: "GET",
            url: "http://admin:asdfg@149.50.143.241:81/stream.jpg",
            //data: {},
            //async: true,
            //contentType: "application/jsonp; charset=utf-8",
            //headers: {
            //    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5',
            //    'Accept': '*/*',
            //    'Authorization': 'Basic ' + auth
            //},
            //timeout: 500,
            dataType: "jsonp",
            //crossDomain: true,
            beforeSend: function (req) {
                req.setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5');
                req.setRequestHeader('Accept', '*/*');
                req.setRequestHeader('Authorization', 'Basic ' + auth);
            },
            success: function (data) {
                alert("Success");
            }
        });

A jsonp response must be wrapped inside a javascript method call. jsonp响应必须包装在javascript方法调用中。 (Callback method). (回调方法)。

Assuming the response is an image. 假设响应是一个图像。 It looks unlikely that jquery will be able to handle it. jQuery似乎不太可能能够处理它。

I do not know if you can use the ajax call to call an image. 我不知道您是否可以使用ajax调用来调用图像。

When you use jsonp the beforeSend at ajax call is ignored. 当您使用jsonp时,ajax调用中的beforeSend将被忽略。

Maybe you have to make your server aware and responsive to CORS. 也许您必须使服务器知道并响应CORS。

Something like this: 像这样:

    protected override void OnStartProcessingRequest(ProcessRequestArgs args)
    {
        HttpContext context = HttpContext.Current;    // set cache policy to this page 

        context.Response.AddHeader("Access-Control-Allow-Origin", "*");
        if (context.Request.HttpMethod == "OPTIONS")
        {
            context.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
            context.Response.AddHeader("Access-Control-Allow-Headers", "X-Requested-With, Accept");
            context.Response.AddHeader("Access-Control-Max-Age", "3628800");
            context.Response.AddHeader("type", "application/json; charset=utf-8");
            context.Response.End();
        }
    }

and use the XDomainRequest and XMLHttpResquest to make the call at client side. 并使用XDomainRequest和XMLHttpResquest在客户端进行调用。

take a look here: http://andre-pedroso.blogspot.pt/2011/02/javascript-consume-service-with-cross.html 在这里看看: http : //andre-pedroso.blogspot.pt/2011/02/javascript-consume-service-with-cross.html

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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