0

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

Json-Fighter
  • 47
  • 1
  • 1
  • 10
  • Can you try using WebMessageBodyStyle.Bare? https://stackoverflow.com/questions/32098418/producing-json-from-c-webmessagebodystyle-wrapped-or-webmessagebodystyle-bare – Oguz Ozgul Oct 31 '17 at 08:54
  • @OguzOzgul,after change i am getting below response {readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …} – Json-Fighter Oct 31 '17 at 09:00
  • where is the response? This does not look like a response to me. – Oguz Ozgul Oct 31 '17 at 09:11
  • this i what i get on console.log(date) "{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}" – Json-Fighter Oct 31 '17 at 09:13
  • @OguzOzgul,on google chrome network i get following ""[{\"usercode\":2},{\"usercode\":23},{\"usercode\":24},{\"usercode\":25},{\"usercode\":26},{\"usercode\":27},{\"usercode\":28},{\"usercode\":29},{\"usercode\":30},{\"usercode\":31}]"" – Json-Fighter Oct 31 '17 at 09:14
  • Oh, you are returning a string, which means you are converting the return value to json yourself, and, also by specifying [return: MessageParameter(Name = "success")], you are causing the return to be like `success: [your array of items]`. Can't you just return the collection itself? – Oguz Ozgul Oct 31 '17 at 09:15
  • @OguzOzgul, why i am getting "{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}" ??? – Json-Fighter Oct 31 '17 at 09:15
  • I think that response is what you expect? Do you still get the error? – Oguz Ozgul Oct 31 '17 at 09:15
  • Where are you getting that "{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}" – Oguz Ozgul Oct 31 '17 at 09:17
  • @OguzOzgul,please check method i am converting datatable to json below is my method – Json-Fighter Oct 31 '17 at 09:17
  • i am using javascript serlization – Json-Fighter Oct 31 '17 at 09:18
  • @OguzOzgul,System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); – Json-Fighter Oct 31 '17 at 09:19
  • @OguzOzgul,i am getting when i am trying "success: function (data) { console.log(data);//i am getting Error }," – Json-Fighter Oct 31 '17 at 09:20
  • @OguzOzgul,Sorry it was coming from ajax error error: function (xhr) { //console.log(xhr); console.log(xhr); } – Json-Fighter Oct 31 '17 at 09:34
  • Answer seem to have nothing to do with the question - so likely something unrelated to the question was causing the issue. Voting to close as "resolved in a manner not likely to help future visitors". – Alexei Levenkov Jan 29 '18 at 02:19

1 Answers1

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();
    }
}
Muhammad Rashid
  • 563
  • 1
  • 6
  • 25