$.ajax({
url: destUrl,
data: ko.toJSON(DataToPost()),
type: "POST",
success: function (result) {
// I am doing my stuff here.
}
},
error: function (request, textStatus, errorThrown) {
alert(request.statusText);
}
});
Here you will notice the data being sent is "ko.toJSON(DataToPost())". DataToPost() returns the XML string contained in my KO model data member DataToPost. ko.toJSON() is used to convert the KO model data in JSON.
This throws a runtime exception which is normally thrown when we post anything containing javascript or html tags. This is a security feature by .NET. Following is the exception.
A potentially dangerous Request.Form value was detected from the client.
<SearchCriteria><Criteria SearchOn="...est 3\" /></SearchCriteria>
I faced similar situation sometimes back (though I was not using KO that time and it was normal post i.e. non Ajax). I overcame the problem that time by using javascript escape() but this time it failed. I used it as follows
$.ajax({
url: destUrl,
data: ko.toJSON(escape(SearchCriteria())),
type: "POST",
success: function (result) {
// I am doing my stuff here.
}
},
error: function (request, textStatus, errorThrown) {
alert(request.statusText);
}
});
Other popular fix is to disable this security either at page level or at application level. It could be done as follows.
Basically, as it is an XML string, you can not convert it to JSON format directly. So the XML's syntaxes are posted to server that causes the security warning. Look at tools that convert XML to JSON, for example,
http://goessner.net/download/prj/jsonxml/
Thank you for replying. I too realized what you have said. Hence instead of XML formatted string I decided to post List of Key value pair instead. This approach is working fine.
Marked as answer by BU XI - MSFT on May 28, 2012 06:05 AM
om singh
Member
52 Points
61 Posts
Not able to Ajax post knockout (KO) data in XML format
May 20, 2012 04:32 PM|LINK
I am working on .NET 4.0 MVC project. I am trying to Ajax post a piece of data from my KO model. That data is a string in following XML format.
I am trying to post it like this.
$.ajax({ url: destUrl, data: ko.toJSON(DataToPost()), type: "POST", success: function (result) { // I am doing my stuff here. } }, error: function (request, textStatus, errorThrown) { alert(request.statusText); } });Here you will notice the data being sent is "ko.toJSON(DataToPost())". DataToPost() returns the XML string contained in my KO model data member DataToPost. ko.toJSON() is used to convert the KO model data in JSON.
This throws a runtime exception which is normally thrown when we post anything containing javascript or html tags. This is a security feature by .NET. Following is the exception.
I faced similar situation sometimes back (though I was not using KO that time and it was normal post i.e. non Ajax). I overcame the problem that time by using javascript escape() but this time it failed. I used it as follows
$.ajax({ url: destUrl, data: ko.toJSON(escape(SearchCriteria())), type: "POST", success: function (result) { // I am doing my stuff here. } }, error: function (request, textStatus, errorThrown) { alert(request.statusText); } });Other popular fix is to disable this security either at page level or at application level. It could be done as follows.
Page level.
Application level.
<configuration> <system.web> <pages validateRequest="false" /> </system.web> </configuration>But doing away with this security is not good. I don't think it will be a wise move. Right now I don't know what to do. Need suggestions.
Thanks in advance!
ajax
BU XI - MSFT
All-Star
22367 Points
2704 Posts
Microsoft
Re: Not able to Ajax post knockout (KO) data in XML format
May 22, 2012 07:01 AM|LINK
Hello
Basically, as it is an XML string, you can not convert it to JSON format directly. So the XML's syntaxes are posted to server that causes the security warning. Look at tools that convert XML to JSON, for example, http://goessner.net/download/prj/jsonxml/
ajax
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
om singh
Member
52 Points
61 Posts
Re: Not able to Ajax post knockout (KO) data in XML format
May 24, 2012 09:50 AM|LINK
Thank you for replying. I too realized what you have said. Hence instead of XML formatted string I decided to post List of Key value pair instead. This approach is working fine.