【发布时间】:2026-01-24 00:50:01
【问题描述】:
我知道这个问题似乎已经发布了很多次,但我几乎阅读了所有这些(互联网上的大部分教程),但我仍然无法理解我做错了什么。
我试图在一个网站中实现我们正在开发一个由 jQuery 脚本使用的 WCF Web 服务,但是在执行 AJAX 请求时我不断收到400 Bad Request,我开始失去希望了。
请注意,我是 WCF 的新手,而且我只是通过在线教程才形成自己的,所以我完全有可能忽略或搞砸了一些事情。
我试过但没有帮助的问题:
- WCF Service returns 400 Bad Request
- uploading large xml to WCF REST service -> 400 Bad request
- 400 Bad Request HTTP Response using a WCF POST via JQuery
- Error 400 (Bad Request) with WCF Tutorial?
- Why does my C# client, POSTing to my WCF REST service, return (400) Bad Request?
我阅读的外部资源无济于事:
- http://www.west-wind.com/weblog/posts/324917.aspx
- http://www.c-sharpcorner.com/UploadFile/sridhar_subra/116/
- http://learningbyfailing.com/2008/05/calling-wcf-from-jquery-using-parameters/
- http://iainjmitchell.com/blog/?p=97
- 许多其他...
我也尝试创建一个新的解决方案,只有一个页面和服务,以排除干扰,但我仍然遇到同样的问题。在这里你可以找到代码:
IService.cs
namespace WebService
{
using System;
using System.ServiceModel;
using System.ServiceModel.Web;
[ServiceContract(Name = "Service", Namespace = "WebService")]
public interface IService
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
String Test();
}
}
Service.svc.cs
namespace WebService
{
using System;
public class Service : IService
{
public String Test()
{
return "Hello, world.";
}
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebService.Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#doAjax").click(function (event) {
event.preventDefault();
jQuery.ajax({
contentType: "application/json"
, dataType: "text"
, error: function (jqXHR, textStatus, errorThrown) {
console.group("AJAX error:");
console.debug(jqXHR);
console.debug(textStatus);
console.groupEnd();
}
, processData: false
, success: function (data, textStatus, jqXHR) {
console.group("AJAX success:");
console.debug(data);
console.debug(textStatus);
console.debug(jqXHR);
console.groupEnd();
}
, type: "post"
, url: "/Service.svc/Test"
});
});
});
</script>
<title>WebService</title>
</head>
<body>
<form runat="server">
<h1><%= this.Page.Title %></h1>
<p><input id="doAjax" type="button" value="Run" /></p>
</form>
</body>
</html>
Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings />
<client />
<behaviors>
<endpointBehaviors>
<behavior name="Behavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="Service">
<endpoint behaviorConfiguration="Behavior" binding="webHttpBinding" contract="WebService.IService" />
</service>
</services>
</system.serviceModel>
</configuration>
【问题讨论】:
-
你想出这个问题的答案了吗?我遇到了同样的问题,找不到解决方案... :(
-
@NaveedButt 不,我后来转到其他项目。试试下面的答案,如果你能做到,请发布答案/评论,以便其他人知道该怎么做。
-
我可以通过在页面中放置一个 div 并将其 html 设置为 jqXHR 来查看有关错误的更多详细信息,以防引发错误。这样我就得到了更好的输出。当我找到一个 IA 时,我会在这里发布一个解决方案。
-
我发现了错误,它与我的javascript有关。我用来调用服务的
jquery.ajax方法出现了一些错误。
标签: jquery asp.net ajax wcf c#-4.0