C# 在WCF JSON调用中获取错误
无论我做什么,jQueryAjax调用都会出错。你能找出我在这里遗漏了什么吗C# 在WCF JSON调用中获取错误,c#,jquery,ajax,json,wcf,C#,Jquery,Ajax,Json,Wcf,无论我做什么,jQueryAjax调用都会出错。你能找出我在这里遗漏了什么吗 <!-- To avoid disclosing metadata information, set the values below to false before deployment --> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> <!-- To receive
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
这是我的界面IShow
[ServiceContract]
public interface IShow
{
[OperationContract]
string getShow(int _showID);
}
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
这是我的班级
[DataContract]
public class Show
{
private string _title, _description;
private DateTime _showDate;
private int _showID;
[DataMember]
public string Title
{
get { return _title; }
set { _title = value; }
}
[DataMember]
public string Description
{
get { return _description; }
set { _description = value; }
}
[DataMember]
public DateTime ShowDate
{
get
{
return _showDate;
}
set { _showDate = value; }
}
[DataMember]
public int ShowID { get { return _showID; } set { _showID = value; } }
}
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
我的服务名为sShows.svc
[AspNetCompatibilityRequirements(
RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class sShows : IShow
{
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json
)]
public string getShow(int _showID)
{
MySqlConnection Conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["CS1"].ConnectionString);
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = Conn;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = " SELECT s.master_id, m.showname, m.showinfo, MIN(CONCAT(s.show_date, ' ', s.show_time)) AS first_show_date, MAX(CONCAT(s.show_date, ' ', s.show_time)) AS last_show_date FROM shows s LEFT OUTER JOIN master m ON s.master_id = m.master_id WHERE (s.wt_offsale = '') AND (s.master_id = @mid) GROUP BY s.master_id ORDER BY s.show_date, s.show_time";
cmd.Parameters.AddWithValue("@mid", _showID.ToString());
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
try
{
Conn.Open();
da.Fill(dt);
Show sh = null;
if (dt.Rows.Count > 0)
{
sh = new Show
{
Title = dt.Rows[0].ItemArray[1].ToString(),
Description = dt.Rows[0].ItemArray[2].ToString()
};
}
// return sh;
return JsonConvert.SerializeObject(sh, Formatting.Indented);
}
catch (Exception)
{
throw;
}
finally
{
Conn.Close();
dt.Dispose();
da.Dispose();
}
}
}
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
最后这是我的web.config(在服务项目中)
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
无论我做什么,我都会收到客户机上的“错误”作为警报。你能查查到底是什么问题吗
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
谢谢
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
更新
这是我从fiddler那里得到的
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
OPTIONS http://localhost:10677/sShows.svc/Web/getShow HTTP/1.1
Host: localhost:10677
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:2299
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: */*
DNT: 1
Referer: http://localhost:2299/SimiArts/events.aspx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,ur;q=0.6
HTTP/1.1 405 Method Not Allowed
Cache-Control: private
Allow: POST
Content-Length: 1565
Content-Type: text/html; charset=UTF-8
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcQXRoYXJBbmlzXERvY3VtZW50c1xNRUdBXFBQSCBKb2JzXFNoYXVuXFdjZlNlcnZpY2UxXHNTaG93cy5zdmNcV2ViXGdldFNob3c=?=
X-Powered-By: ASP.NET
Date: Tue, 08 Apr 2014 16:12:24 GMT
<?xml version="1.0" encoding="utf-8"?>
<!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>
<title>Service</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>
<div id="content">
<p class="heading1">Service</p>
<p>Method not allowed.</p>
</div>
</body>
</html>
选项http://localhost:10677/sShows.svc/Web/getShow HTTP/1.1
主机:localhost:10677
连接:保持活力
访问控制请求方法:POST
来源:http://localhost:2299
用户代理:Mozilla/5.0(Windows NT 6.3;WOW64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/33.0.1750.154 Safari/537.36
访问控制请求头:接受,内容类型
接受:*/*
DNT:1
推荐人:http://localhost:2299/SimiArts/events.aspx
接受编码:gzip、deflate、sdch
接受语言:en-US,en;q=0.8,ur;q=0.6
不允许使用HTTP/1.1 405方法
缓存控制:专用
允许:邮寄
内容长度:1565
内容类型:text/html;字符集=UTF-8
服务器:Microsoft IIS/8.0
X-AspNet-Version:4.0.30319
X-SourceFiles:=?UTF-8?B?QZPCVxNLCNNCQXROYXJBBMLZXERVY3VTZW50C1XNRUDBXFBQSCBKB2JZXFNOYXVUXFDJZLCNZY2UXHNTAG93CY5ZDMNC2VIXGDLDFNOB3C==
X-Powered-By:ASP.NET
日期:2014年4月8日星期二16:12:24 GMT
服务
正文{color:#000000;背景色:白色;字体系列:Verdana;左边距:0px;右边距:0px;}内容{35;左边距:30px;字体大小:70em;底部填充:2em;}A:链接{color:#336699;字体重量:粗体;文本装饰:下划线;}A:已访问{color:#6699cc;字体重量:粗体;文本装饰:下划线;}A:活动{颜色:#336699;字体大小:粗体;文本装饰:下划线;}。标题1{背景颜色:#003366;边框底部:#336699 6px实心;颜色:#FFFFFFFF;字体系列:Tahoma;字体大小:26px;字体大小:正常;边距:0em 0em 10px-20px;填充底部:8px;填充左侧:30px;填充顶部:16px;}预处理{字体大小:小;背景色:#e5e5cc;填充:5px;字体系列:Courier New;页边距顶端:0px;边框:1px#f0f0e0实心;空白:预换行;空白:-预换行;字换行:中断字;}表格{边框折叠:折叠;边框间距:0px;字体系列:Verdana;}表格th{右边框:2px白色实心;边框底部:2px白色实心;字体大小:粗体;背景色:#cecf9c;}表格td{右边框:2px白色实心;边框底部:2px白色实心;背景色:#e5e5cc;}
服务
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
方法不允许
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
“{u showID:288}”
不是。“{\”\u showID\:288}”
可能就是您要找的。或者,更好的做法是,创建一个普通对象,让jQuery进行JSON序列化
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
data: { _showId: 288 },
选项调用是由跨站点脚本调用引起的。请将网站和服务置于同一主机和端口上,或使用建议的回调。我想从公共API的角度补充一点,即在属性名称中使用下栏也不是那么直观/友好。偏爱“showId”而不是“showId”尝试了一下,但没有任何运气:(@TheSenator我已经添加了Fiddler Login,我敢肯定,这一定与网络有关。config@AtharAnis请提供更多详细信息:是发生了
成功
还是错误
块,写入日志的是什么,如果使用调试器,是否正在进入getShow
。错误详细信息比大量的(大部分不相关)代码。正在执行的是错误块…我得到错误“方法不允许”
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehaviour">
<!--<enableWebScript/>-->
<webHttp defaultOutgoingResponseFormat="Json"/>
<!--<webHttp/>-->
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>