在jsonP数据上获取错误

时间:2017-10-31 08:02:24

标签: c# json wcf jsonp

我想获取jsonp数据,但我收到错误

$.ajax({
                type: "GET",
                url: 'http://localhost:59672/RestServiceImpl.svc/getallemp',
                contentType: "application/json",
                dataType: "jsonp",
                jsonpCallback: 'callback',
                success: function (data) {
                    console.log(data);//i am getting Error

                },
                error: function (xhr) {
                    console.log(xhr);
                }
            });

下面是我的json回复

{"success":"[{\"usercode\":2},{\"usercode\":23},{\"usercode\":24},{\"usercode\":25},{\"usercode\":26},{\"usercode\":27},{\"usercode\":28},{\"usercode\":29},{\"usercode\":30},{\"usercode\":31}]"}

然后我得到以下错误

Uncaught SyntaxError: Unexpected token :

我正在使用WCF Restful Service 以下是服务代码

[WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "getallemp")]
        [return: MessageParameter(Name = "success")]
        string GetAllEmployee();

Response

1 个答案:

答案 0 :(得分:1)

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin" , "*");
    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    }
}