简体   繁体   中英

Accessing cross-domain data using jsonp

Well, I'm trying to access php web service (returning jsonp and url format is http://service.com/service.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.

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