开发者

Accessing cross-domain data using jsonp

Well, I'm trying to access php web service (returning jsonp and url format is http://service.com/s开发者_StackOverflow中文版ervice.jsonp). First it was failing silently. So, I tried debugging following code in direct Visual Studio and IE.

<script type="text/javascript">
    $(document).ready(function () {
        $('#btnClick').click(function () {
            $.ajax({
                url: "http://url.jsonp",
                dataType: "jsonp",
                jsonp: "data",
                jsonpCallback: "jsonpcallback"
            });

            function jsonpcallback(data) {
                alert('doinng it now');
            }
        });
    });
</script>

After click, the data comes in VS (which is a correct jsonp output) but VS throws an error. Following is the result:

Copy code

data({"code":001,"msg":"true","data":{"obj1":"val1","obj2":"val2"}})

Error in VS: Microsoft JScript runtime error: 'data' is undefined


It seems, that the return from the server is wrong. Given your jQuery parameters, the result should look like jsonpcallback({...}). Can you check, that the requested URL is this:

 http://url.jsonp/?data=jsonpcallback

If it is, the server-side does it wrong and mixes the GET parameter name with its value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜