Javascript WCF web服务从AJAX发送JSON
嘿,我现在的代码需要一些帮助。我想通过jQuery AJAX调用将值2767994111发送到我的WCF web服务Javascript WCF web服务从AJAX发送JSON,javascript,jquery,ajax,vb.net,wcf,Javascript,Jquery,Ajax,Vb.net,Wcf,嘿,我现在的代码需要一些帮助。我想通过jQuery AJAX调用将值2767994111发送到我的WCF web服务 var parameter = { value: "2767994111" }; $('#btSubmit').click(function () { $.ajax({ url: "http://localhost:65234/Service1.svc/GetData/", data: JSON.stringify(parameter),
var parameter = { value: "2767994111" };
$('#btSubmit').click(function () {
$.ajax({
url: "http://localhost:65234/Service1.svc/GetData/",
data: JSON.stringify(parameter),
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
});
IService1.vb
<ServiceContract> _
Public Interface IService1
<OperationContract> _
<WebGet(UriTemplate:="GetData/{value}", RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.WrappedRequest)> _
Function getEmpData(value As String) As String
End Interface
web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
<bindings>
<webHttpBinding>
<binding name="Binding" crossDomainScriptAccessEnabled="true">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
<binding name="httpbind" crossDomainScriptAccessEnabled="true">
</binding>
</webHttpBinding>
</bindings>
<client />
<services>
<service name="Wcf.App.Service1" behaviorConfiguration="ServiceBehaviour">
<endpoint address=""
binding="webHttpBinding"
bindingConfiguration="httpbind"
contract="Wcf.App.IService1"
behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="web">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
转到:
var parameter = { value: "2767994111" };
$('#btSubmit').click(function () {
$.ajax({
url: "http://localhost:65234/Service1.svc/GetData/" + parameter.value,
data: JSON.stringify(parameter),
type: "GET",
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
});
“/”应用程序中出现服务器错误
找不到资源
描述:HTTP404。您正在寻找的资源(或其
依赖项)可能已被删除、名称已更改或
暂时不可用。请查看下面的URL并进行修改
确保它拼写正确
请求的URL:/Service1.svc/GetData/2767994111
在发出GET请求时指定
application/json
内容类型没有意义,因为请求的主体是空的。也不要JSON.stringify
,而是按照服务的预期在url中发送值:
var parameter = { value: "2767994111" };
$('#btSubmit').click(function () {
$.ajax({
url: "http://localhost:65234/Service1.svc/GetData/" + parameter.value,
type: "GET",
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
});
更新: 看起来您的
Service1
类没有实现IService1
接口,这就是您获得这些404的原因。因此,请确保正确实现服务接口:
Public Class Service1
Implements IService1
Public Function getEmpData(phoneNum As String) As String Implements IService1.getEmpData
... snip
End Function
End Class
还是不行。我现在在AJAX上得到了500(内部服务器错误)(匿名函数)。问题是您的
Service1
类没有实现IService1
服务契约。我更新了我的答案,以说明如何做到这一点。正确执行服务合同后,您应该能够在浏览器中直接键入以下url并收到预期结果:http://localhost:65234/Service1.svc/GetData/2767994111
。此外,我还建议您在实际实现该方法之前,只设置服务的框架并测试其工作情况。这样,调试和隔离问题就容易多了。这次添加时没有错误,但也没有数据。这只是一个空白页。即使我告诉它输出虚假数据,它仍然是一个空白页?您是否从getEmpData
方法中删除了所有实现细节,只是在其中执行了一个空返回“FooBar”
语句?完成此操作后,是否键入http://localhost:65234/Service1.svc/GetData/2767994111
浏览器地址栏中的url?
Public Class Service1
Implements IService1
Public Function getEmpData(phoneNum As String) As String Implements IService1.getEmpData
... snip
End Function
End Class