Jquery Cross-Domain ajax call using JSONP

Hi everyone. We all know the role of ajax and its implementation. We can use ajax in asp.net as well as in javascript/jquery. But there is a

Editorial Note

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Hi everyone. We all know the role of ajax and its implementation. We can use ajax in asp.net as well as in javascript/jquery. But there is a limitation of same origin policy. i.e. we can only use ajax to post and get requests within our site. We can call webservices but that is from the code behind only. Even if we use any scripting language it restricts us calling a third party domain. Also over SSL javascript/jquery's ajax call gives up easily. But using jsonp with jquery's ajax api call we can target the ajax call outside the scope of our website.

$(document).ready(function() {
            var surl = "http://www.anotherdomain.com/webservice.asmx";
            $.ajax({
                type: 'GET',
                url: surl,
                crossDomain: true,
                contentType: "application/json; charset=utf-8",
                data: { UserID: 1234 },
                dataType: "jsonp",
                success: function(msg) {
                    $.each(msg, function(name, value) {
                        alert(value);
                    });
                },
                error: function(xhr, status, error) { alert('Servidor de error 404 !!'); },
                async: false,
                cache: false
            });
        });

Note:- This will only work with json data type, so while requesting a web service should return the data in Javascript Object notation format.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值