WCF AJAX调用不工作与jQuery $阿贾克斯 [英] WCF Ajax Call not working with Jquery $.ajax
问题描述
我有以下的jQuery(服务名称变更):
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!'); }
});
以上发出请求到服务器是在Sitefinity我的本地IIS7.5服务器上的WCF服务。下面是相关的web.config中:
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>
...
最后,接口和实现为MyService的:
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();
}
}
我想这就是一切。无论如何,$就调用返回成功,但Web服务方法并没有被所谓的(我有一个断点设置)。 我打开了小提琴手,发现我被赋予了405:不允许的方法。我已经看到过,但只有当我忘了建立方法,以允许POST请求。我很困惑,为什么它现在这样做。
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.
此外,奇怪的是,如果我在克隆小提琴手捕获Ajax请求,我得到如下:
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-Control-Request-Method: POST
就在头,没有请求主体可言。
Just the header, no request body to speak of.
推荐答案
看看我的博客中找到的教程:的http://sameproblemmore$c$c.blogspot.nl/2011/10/creating-secure-restfull-wcf-service.html 它containst它使用jQuery的AJAX GET和POST,跨域和基本身份验证,以确保端点。
Check out the tutorial found at my blog: http://sameproblemmorecode.blogspot.nl/2011/10/creating-secure-restfull-wcf-service.html It containst an example which uses jquery ajax GET and POST, cross domain and basic authentication to secure the endpoint.
这篇关于WCF AJAX调用不工作与jQuery $阿贾克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!