Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery Ajax post to WCF REST Service ABORTED every time
    text
    copied!<p>I have a WCF REST service that I created using the WCF REST Service Template 40. I can successfully get a JSON response using Fiddler, but I can't get a response using a jQuery ajax call. The request is aborted every time.</p> <p>On the WCF side, here is the method in question:</p> <pre><code> [WebInvoke(UriTemplate = "GetMedicalEntities", Method = "*", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] [OperationContract] public IEnumerable&lt;MedicalEntityResult&gt; GetMedicalEntities(MedicalEntityRequest request) { if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS") { WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST"); WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept"); return null; } else { IEnumerable&lt;MedicalEntity&gt; matchingMedicalEntities = LTSTest_DataAccess.GetMedicalEntitiesWithName(request.FirstPartOfName); IEnumerable&lt;MedicalEntityResult&gt; medicalEntityResults = matchingMedicalEntities.Select(m =&gt; new MedicalEntityResult { HospitalName = m.EntityName, //Address = m.Addresses.Where(a =&gt; a.AddressTypeId == 1).FirstOrDefault().Address1 }).ToList(); return medicalEntityResults; } } </code></pre> <p>You'll notice that I'm checking for requests that use the "OPTIONS" method. I thought that might have been part of my problem after reading this: <a href="https://stackoverflow.com/questions/4875195/problem-sending-json-data-from-jquery-to-wcf-rest-method">Problem sending JSON data from JQuery to WCF REST method</a></p> <p>Here are the attributes for my service class, btw:</p> <pre><code> [ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] // NOTE: If the service is renamed, remember to update the global.asax.cs file public class CreditAuthRESTService { </code></pre> <p>This is how the request body needs to be formatted (this is what works in Fiddler at least):</p> <pre><code>{"FirstPartOfName":"Med "} </code></pre> <p>And here is the client-side test code where we attempt to consume the service:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { $("#txt_search").keyup(function() { var search; search = $("#txt_search").val(); if (search.length &gt; 2) { event.preventDefault(); // Trigger AJAX request $.ajax( { type: "POST", url: "XXXXXXXXXXXX", dataType: 'json', contentType: 'application/json charset UTF-8', data: '{"FirstPartOfName":"' + search + '"}', success: function(message) { if (message.length &gt; 0) { alert('It got data back....'); message = "Do you mean: " + message; $("#suggest").append(message); } else { alert('Nothing came back....'); } } } ); } else { // Empty suggestion list $("#suggest").empty(); } }); }); </code></pre> <p></p> <p>The idea with this is to implement an auto-complete feature, so that users can see a collection of medical entities whose names begin with the characters that have been entered. </p> <p>We inserted the event.PreventDefault() just before the Ajax request after reading the second answer to this post: <a href="https://stackoverflow.com/questions/2241861/firebug-1-5-showing-200-aborted-for-ajax-requests">Firebug 1.5 showing 200 Aborted for Ajax requests</a></p> <p>However, it didn't make a difference. Once again, all the requests are aborted. Would anyone have an idea as to what might be going on? I can include more client or service-side details if necessary.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload