我将尝试向服务端点进行POST请求,服务映射请求正确,但完全没有激活RequestDTO。
服务方法
public object Post(PostUpdateContactRequest request)
{
// Breakpoint below, has hit with no problem. Everything is in the request null though.
Contacts upd_contact = Contacts.Load(request.Contactid);
//..other vlaidation/code/etc
this.m_repository.PostEditContact(upd_contact);
return new HttpResult() { StatusCode = System.Net.HttpStatusCode.Created };
}
请求DTO
[Route("/contact/update/","POST")]
public class PostUpdateContactRequest
{
public virtual long Contactid { get; set; }
public virtual string Firstname { get; set; }
public virtual string Lastname { get; set; }
public virtual string Middlename { get; set; }
public virtual string Suffix { get; set; }
public virtual string Homephone { get; set; }
public virtual string Mobilephone { get; set; }
public virtual string Workphone { get; set; }
public virtual string Addressline1 { get; set; }
public virtual string Addressline2 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string Zipcode { get; set; }
public virtual string Nickname { get; set; }
public virtual string Email { get; set; }
}
JSON数据(生成的数据并非真实数据)
{ "Addressline1" : "Ap #638-3472 Dolor. Road",
"Addressline2" : "",
"City" : "Forest Lake",
"Contactid" : "1",
"Email" : "nibh.sit.amet@gravida.ca",
"Firstname" : "Amela",
"Homephone" : "222-222-2222",
"Lastname" : "Airheart",
"Middlename" : " S",
"Mobilephone" : "1-111-111-1111",
"Nickname" : "Thomas",
"State" : "TN",
"Suffix" : "NA",
"Workphone" : "(888)-888-8888",
"Zipcode" : "32549"
}
我有所遗漏吗?
更多研究后的更新:
看起来我的Jquery客户端中的ContentType + CORs + Datatype彼此之间存在冲突。在阅读了许多关于CORS的文章后(它确实存在于这里),每个人都建议使用'jsonp'作为contentType,这会导致HTTP OPTIONS发生,而我的服务断点不再被命中。显然,因为动词不再匹配。
将contentType切换为'json'会导致GET请求。同样,动词不匹配。我的服务期望进行POST请求。
所有这些都是为了尝试发布JSON对象。
使用:
$.ajax({
url: post_url,
type: 'POST',
data: jdata,
dataType: 'json',
contentType: 'application/json',
success: function (data) {
alert('updated!');
},
error: function (xhr, textStatus, error) {
alert(xhr);
alert(textStatus);
alert(error);
}
});
如果我删除我添加的内容,让jquery决定这些选项,我会再次得到一个POST请求,但没有JSON对象。它将内容类型切换为:Content-Type application/x-www-form-urlencoded; charset=UTF-8。
我的服务不会寻找这种类型的内容,会忽略它,并且从未对任何内容进行反序列化。但是该对象正在尝试发布!以下是标题:
请求头
POST /AerosMobileInterface/contact/update/ HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:7072/AjaxDetail.htm?Contactid=98
Content-Length: 387
Origin: http://localhost:7072
Pragma: no-cache
Cache-Control: no-cache
响应头
HTTP/1.1 500 NullReferenceException
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: ServiceStack/3.932 Win32NT/.NET, ASP.NET
X-AspNet-Version: 4.0.30319
access-control-allow-headers: content-type
Access-Control-Allow-Origin: *
Date: Sun, 06 Jan 2013 17:13:30 GMT
Content-Length: 9914
POST参数: { "Addressline1" : "Ap #638-3472 Dolor. Road", "Addressline2" : "", "City" : "Forest Lake", "Contactid" : "1", "Email" : "nibh.sit.amet@gravida.ca", "Firstname" : "Amela", "Homephone" : "222-222-2222", "Lastname" : "Airheart", "Middlename" : " S", "Mobilephone" : "1-111-111-1111", "Nickname" : "Thomas", "State" : "TN", "Suffix" : "NA", "Workphone" : "(888)-888-8888", "Zipcode" : "32549" }
看起来我无法让内容类型正常工作!我真的不想修改Jquery、HTTP和ServiceStack,来完成这么简单的事情。有很多SO帖子中的人们都遇到了类似的问题,在$.ajax函数中更改contentType和DataType对他们有效,但在这里却不起作用。
谢谢
更新: - jdata是作为$.ajax调用中的数据参数传递的变量,是一个{}对象。我使用console.log(jdata)验证了它确实是一个对象,并且没有被误认为是字符串或其他可能会误导$.ajax的类型。Firebug似乎认为它是一个有效的对象,并生成该对象的正确JSON投影。
看起来$.ajax试图将该对象放在REST调用的查询字符串中。我能够更改我的服务,以便它不再接受查询字符串中的值。这会导致服务方法不起作用。
Access-Control-Allow-Origin
头,则无法完成。使用jsonp无法进行POST,而使用常规ajax进行POST需要CORS头或您的URL位于同一域中。 - Dynalon