[英]Calling WCF service method from jQuery AJAX returns 404
This is my jQuery AJAX method: 这是我的jQuery AJAX方法:
$(document).ready(function() {
$("#btnSubmit").click(function() {
alert("Test");
var param = $("#txtEmail").val();
$.ajax({
url:"~/DemoService1.svc/IsEmailAvailable",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
//data: JSON.stringify({ Email: $('#txtEmail').val() }),
data: param,
success: function(msg) { //On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed
});
});
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + ' ' + result.statusText);
Type = null;
varUrl = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;
}
});
Here is my service interface. 这是我的服务界面。
[OperationContract]
[WebInvoke(UriTemplate = "/IsEmailAvailable", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
bool IsEmailAvailable(string Email);
My webconfig is look like this: 我的webconfig看起来像这样:
<client>
<endpoint address="http://localhost:56537/DemoService1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IDemoService1" contract="DemoService.IDemoService1"
name="BasicHttpBinding_IDemoService1" />
</client>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="DemoService">
<endpoint address="" binding="webHttpBinding" contract="IDemoService1" behaviorConfiguration="EndBehaviour"></endpoint>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
Please provide the solution. 请提供解决方案。 I am new in jQuery.
我是jQuery的新手。
It looks like the issue is the URL in your AJAX call: 看起来问题是您的AJAX调用中的URL:
url:"~/DemoService1.svc/IsEmailAvailable",
Notice that you are beginning the URL with the tilde (~) character. 请注意,您使用波形符(〜)字符开始URL。 The tilde is an ASP.NET token meaning "Application Root".
代字号是一个ASP.NET令牌,意思是“应用程序根”。 JQuery doesn't know what this means, so the ajax call is going to something like " http://mysite/~/DemoService1.svc/IsEmailAvailable " which probably isn't a valid URL.
JQuery不知道这意味着什么,所以ajax调用会像“ http://mysite/~/DemoService1.svc/IsEmailAvailable ”那样可能不是有效的URL。
Replace the url
parameter with a valid URL and the 404 will likely go away. 将
url
参数替换为有效的URL,404可能会消失。 Good tools for troubleshooting this are the developer console in your browser or Fiddler. 用于解决此问题的好工具是浏览器或Fiddler中的开发人员控制台。 I've found both to be invaluable when troubleshooting why client-side calls aren't working properly (trust me... you are going to deal with plenty of HTTP response codes, and you'll want a tool that allows you to view exception details).
我发现在解决为什么客户端调用无法正常工作时这两个都非常宝贵(相信我......你将处理大量的HTTP响应代码,并且你需要一个允许你查看的工具例外细节)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.