How can I produce JSONP from an ASP.NET web service for cross-domain calls?

后端 未结 3 789
心在旅途
心在旅途 2020-11-29 19:47

I\'ve written a webservice which returns JSON and I\'ve tried to call it using jQuery like this:

$.ajax({
    contentType: \"application/json; charset=utf-8\         


        
3条回答
  •  时光说笑
    2020-11-29 20:29

    Thanks Nick, that was an excellent answer to a problem that I too had a hard time finding at first online. Worked great for me as well.

    Wanted to make sure this this line of post got the attention it deserves.

    Just wanted to add that I used the built in serializer (System.Runtime.Serialization.Json) and it worked like a charm as well.

            List orderHistory = null;
    
            StringBuilder sb = new StringBuilder();
            JavaScriptSerializer js = new JavaScriptSerializer();
            sb.Append(callback + "(");
            sb.Append(js.Serialize(orderHistory));
            sb.Append(");");
    
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            Context.Response.Write(sb.ToString());
            Context.Response.End();
    

提交回复
热议问题