I have the following jQuery (service name altered):
var url = "http://localhost/services/MyService.svc/addentrant";
var stuff = $("#signup-form").serializeArray();
$.ajax({
type: "POST",
url: url,
contentType: "application/json; charset=utf-8",
data: stuff,
timeout: 10000,
success: function (obj) { alert('yay!'); }
});
The above makes a request to a WCF service hosted in Sitefinity on my local IIS7.5 server. Below is the relevant web.config:
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp/>
</behavior>
...
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
...
<services>
<service behaviorConfiguration="DefaultBehavior" name="Services.MyService" >
<endpoint address="" behaviorConfiguration="jsonBehavior" binding="webHttpBinding" contract="Services.IMyService" bindingConfiguration=""/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
...
Finally, the interface and implementation of MyService:
[ServiceContract(Name = "MyService", Namespace = "http://myservice.com/services/2010/")]
public interface IMyService
{
[OperationContract,
WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "addentrant")]
void AddEntrant(string firstName);
}
...
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyervice
{
...
public void AddEntrant(string firstName)
{
Entrant entrant = new Entrant()
{
FirstName = firstName,
};
context.Entrants.InsertOnSubmit(entrant);
context.SubmitChanges();
}
}
I think that's everything. Anyway, the $.ajax call returns a success, but the web service method was not being called (I had a breakpoint set). I opened up Fiddler and found I was being given a 405: Method Not Allowed. I've seen that before, but only when I had forgotten to set up the method to allow POST requests. I'm very confused as to why it is doing this now.
Also, oddly enough, if I clone the ajax request captured in Fiddler, I get the following:
OPTIONS /services/MyService.svc/addentrant HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost:6339
Access-Co开发者_运维问答ntrol-Request-Method: POST
Just the header, no request body to speak of.
What happens if you try to use GET instead of POST?
Try clearing your cache or appending a timestamp to the url - the 200 response code may have been cached by the browser
Another thing to try is not setting the contentType
in your $.ajax
call and use dataType: "json"
instead.
$.ajax({
type: "POST",
url: url,
dataType: "json",
data: stuff,
timeout: 10000,
success: function (obj) { alert('yay!'); }
});
精彩评论