c# - Nancy model binding not working in Chrome, IE - Stack Overflow

I have been having this issue in one of my applications and have stripped it down and set up a small te

I have been having this issue in one of my applications and have stripped it down and set up a small test environment in which the problem still occurs.

I am posting the following object (JSON)

{
    "eventName":"Testing from Services",
    "tickets":10,
    "_date":"10/10/2013",
    "_time":"8:00 PM",
    "ticketsLocation":"Testing from Services",
    "date":"2013-10-11T00:00:00.000Z"
}

using the following ajax call

self.save = function (item, url, success) {
    $.ajax({
        type: "post",
        data: JSON.stringify(item),
        contentType: "application/json, charset=utf-8",
        traditional: true,
        datatype: "json",
        url: self.domain + url,
        success: success,
        error: self.error
    });
};

and then binding the data with the following code on the server

var Model = this.Bind<PropertyType>();

where PropertyType is the correct type (Event).

Here is the Event class for reference

public class Event
{
    public string EventName { get; set; }
    public int Tickets { get; set; }
    public Venue Venue { get; set; }
    public string TicketsLocation { get; set; }
    public DateTime Date { get; set; }
    public List<EventRequest> Requests { get; set; }
}

This works perfectly fine in Firefox. In Chrome and IE, Model ends up being an Event object with all null values. As far as I can tell (by using Fiddler), the post request is exactly the same between all browsers. I have also tested this on other machines, ruling out my machine and/or browsers as the issue.

Any ideas? I don't understand how the browser affects Nancy model binding...

I have been having this issue in one of my applications and have stripped it down and set up a small test environment in which the problem still occurs.

I am posting the following object (JSON)

{
    "eventName":"Testing from Services",
    "tickets":10,
    "_date":"10/10/2013",
    "_time":"8:00 PM",
    "ticketsLocation":"Testing from Services",
    "date":"2013-10-11T00:00:00.000Z"
}

using the following ajax call

self.save = function (item, url, success) {
    $.ajax({
        type: "post",
        data: JSON.stringify(item),
        contentType: "application/json, charset=utf-8",
        traditional: true,
        datatype: "json",
        url: self.domain + url,
        success: success,
        error: self.error
    });
};

and then binding the data with the following code on the server

var Model = this.Bind<PropertyType>();

where PropertyType is the correct type (Event).

Here is the Event class for reference

public class Event
{
    public string EventName { get; set; }
    public int Tickets { get; set; }
    public Venue Venue { get; set; }
    public string TicketsLocation { get; set; }
    public DateTime Date { get; set; }
    public List<EventRequest> Requests { get; set; }
}

This works perfectly fine in Firefox. In Chrome and IE, Model ends up being an Event object with all null values. As far as I can tell (by using Fiddler), the post request is exactly the same between all browsers. I have also tested this on other machines, ruling out my machine and/or browsers as the issue.

Any ideas? I don't understand how the browser affects Nancy model binding...

Share Improve this question asked Jul 24, 2013 at 15:13 whololwholol 9282 gold badges9 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

The simple answer is that your content-type is invalid. There is no such thing as an application/json, charset=utf-8 content type, despite what people might tell you. Even though charset is a valid, optional, extension to content-type it does not apply to application/json

You can read about this here http://www.ietf/rfc/rfc4627.txt?number=4627 under section 6 IANA considerations

The MIME media type for JSON text is application/json.

Type name: application

Subtype name: json

Required parameters: n/a

Optional parameters: n/a

With the additional explanation about encoding

Encoding considerations: 8bit if UTF-8; binary if UTF-16 or UTF-32

 JSON may be represented using UTF-8, UTF-16, or UTF-32.  When JSON
 is written in UTF-8, JSON is 8bit patible.  When JSON is
 written in UTF-16 or UTF-32, the binary content-transfer-encoding
 must be used.

In short, JSON is already, implicitly, utf-8. In fact, under section 3. Encoding it states

JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.

Send in application/json and you should be set to go

Hope this helps :-)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745115572a4612107.html

相关推荐

  • c# - Nancy model binding not working in Chrome, IE - Stack Overflow

    I have been having this issue in one of my applications and have stripped it down and set up a small te

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信