It is very simple, run this Silverlight4 example while preserving the ContentType property, and you will get a response from my service in xml. Now uncomment the property and run it, and you will get a ProtocolViolationException, which should happen that the service returns data in JSON format.
#if DEBUG
Address = "http://localhost.:55437/Services/GetFoodDescriptionsLookup(2100)";
#else
Address = "http://stephenpattenconsulting.com/Services/GetFoodDescriptionsLookup(2100)";
#endif
Uri httpSite = new Uri(Address);
HttpWebRequest wreq =
(HttpWebRequest)WebRequestCreator.ClientHttp.Create(httpSite);
wreq.Accept = "application/json";
wreq.BeginGetResponse((cb) =>
{
HttpWebRequest rq = cb.AsyncState as HttpWebRequest;
HttpWebResponse resp = rq.EndGetResponse(cb) as HttpWebResponse;
StreamReader rdr = new StreamReader(resp.GetResponseStream());
string result = rdr.ReadToEnd();
rdr.Close();
}, wreq);
New exception
System.NotSupportedException Message = "" : System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, ) System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) com.patten.silverlight.ViewModels.WebRequestLiteViewModel.b_0 (IAsyncResult cb) System.Net.Browser.BrowserHttpWebRequest. < > c_DisplayClassd.b__b (Object state2) System.Threading.QueueUserWorkItemCallback.WaitCallback_Context ( ) System.Threading.ExecutionContext.Run(ExecutionContext executeContext, ContextCallback, , Boolean ignoreSyncCtx) System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() System.Threading.ThreadPoolWorkQueue.Dispatch() System.Threading.ThreadPoolWaitCallback.PerformWaitCallback() InnerException: System.NotSupportedException Message = . : System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) System.Net.Browser.BrowserHttpWebRequest. < > c_DisplayClass5.b_4 (Object sendState) System.Net.Browser.AsyncHelper. < > c_DisplayClass2.b__0 (Object sendState) InnerException:
, , , , Fiddler , .. http://localhost.:55437/Services/GetFoodDescriptionsLookup(2100), DOT localhost.
!
, , , .. , URI, , SO, . ().
, , , stackoverflow, .
jQuery, , " " "", SL4. ( !)
function CallService(serviceUrl, data, callback) {
$.ajax({
url: serviceUrl,
data: data,
dataType: 'json',
contextType: "application/json",
cache: false,
success: callback,
error: function (msg) {
alert("Request Failed!");
}
});
}