Search code examples
c#jsonwcfjsonp

getting error on jsonP data


I am trying to get jsonp Data but i am getting error

$.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);
                }
            });

below is my json response

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

then i am getting below error

Uncaught SyntaxError: Unexpected token :

i am using WCF Restful Service below is Service code

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

Response


Solution

  • 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();
        }
    }