WCF跨域使用JSONP错误未捕获的SyntaxError:意外的标记: [英] WCF Cross Domain Using Jsonp Error Uncaught SyntaxError: Unexpected token :

查看:306
本文介绍了WCF跨域使用JSONP错误未捕获的SyntaxError:意外的标记:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用Web服务通过交叉使用jquey域

I am trying to call webservice over cross domain using jquey.

下面是我的code调用服务

Here is my code to call service

 $(document).ready(function () {
        $.ajax({
            type: 'GET',
            async: false,
            contentType: "application/json",
            url: "http://localhost:52136/Service1.svc/Helloworld?callback=func_callbk",
            dataType: "jsonp",
            success: function (data) {
                alert('sucesss')
                alert(data.d);
            },
            error: function (data) {
                alert(data);
            }
        });
    });
    func_callback = function (data) {
        alert(data.data.people[0].id);
    }

我从服务返回简单的字符串。

I am returning simple string from the service.

public string HelloWorld()
{
    return "Hello World";
}

服务是由所谓的,但我得到的错误 未捕获的SyntaxError:意外的标记: 镀铬的控制台窗口。

Service is called from, but i am getting error Uncaught SyntaxError: Unexpected token : in console window of chrome.

我得到这个字符串,同时呼吁从浏览服务: {HelloWorldResult:Hello World的}

I am getting this string while calling service from the browse: {"HelloWorldResult":"Hello World"}

请让我知道我要去哪里错了?

Please let me know where am I going wrong?

在此先感谢。

推荐答案

您WCF服务器进行配置,以使 JSONP CrossDomainScriptAccessEnabled

Your WCF server should be configured to enable jsonp with CrossDomainScriptAccessEnabled

下面是一个简单的工作示例

Here is a simple working example

服务器:

Task.Run(() =>
{
    var uri = new Uri("http://0.0.0.0/test");
    var type = typeof(TestService);
    WebServiceHost host = new WebServiceHost(type, uri);
    WebHttpBinding binding = new WebHttpBinding();
    binding.CrossDomainScriptAccessEnabled = true;
    host.AddServiceEndpoint(type, binding, uri);


    host.Open();

});

[ServiceContract]
public class TestService
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public string Hello()
    {
        return "Hello World";
    }
}

这是JavaScript code

And this is the javascript code

<html>
<body>
<script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
<script>
    $(document).ready(function () {
    $.ajax({
        type: 'GET',
        async: false,
        contentType: "application/json",
        url: "http://localhost/test/hello",
        dataType: "jsonp",
        success: function (data) {
            alert(data);
        },
        error: function (data) {
            alert(data);
        }
    });
});

</script>
</body>
</html>

现在,你可以从你的浏览器中测试它

Now you can test it from your browser

http://localhost/test/hello

http://localhost/test/hello?callback=myfunc

这篇关于WCF跨域使用JSONP错误未捕获的SyntaxError:意外的标记:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆