Nancy model binding does not work in Chrome, IE

I had this problem in one of my applications, and I turned it off and set up a small test environment in which the problem still occurs.

I am sending 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 data binding 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 great in Firefox. In Chrome and IE, the Model ends as an Event object with all null values. As far as I can tell (using Fiddler), the mail request is exactly the same between all browsers. I also tested this on other machines, excluding my machine and / or browsers as a problem.

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

+4
source share
1 answer

The simple answer is that your content type is invalid. The content type application/json, charset=utf-8 does not exist even though people can tell you. Although charset is a valid optional extension for the content type, it does not apply to application/json

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

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

Type Name: Application

Subtype Name: json

Required parameters: n / a

Additional parameters: n / a

With additional explanation about coding

Encoding considerations: 8 bits 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 compatible. 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, in section 3. Encoding it indicates

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

Submit application/json and you need to be configured to go

Hope this helps :-)

+8
source

Source: https://habr.com/ru/post/1493233/


All Articles