I’m having difficulty making a dynamic AJAX form submition method for .NET WebServices.
The idea is to send a request that is dynamically built from all the inputs in a form. Then let the server respond with JSON.
This is called when the form is submitted, it fills an array with the indexes as the input field name and the value as the value:
var params = [];
for( var i in inputs )
{
if( inputs[i].type == 'text' || inputs[i].type == 'password' )
{
params[inputs[i].name] = inputs[i].value;
}
}
When the AJAX request is sent I run a loop to produce the request body:
var l = 0;
for( var i in parameters )
{
this.parameters += ( l > 0 ? '&' : '') + i + '=' + parameters[i];
l++;
}
which gives a result like this:
foo=bar&lol=haha
The problem is that script servicse only accepts JSON as the request body because of the decorator:
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
As I want it to return a JSON string rather than XML.
Is there a simple solution?
The solution I came up with is a rather complex work around for something that should be simple. Basically I made a function that accepts Xml format. Then adds data to an update model which is formatted to JSON with an update method which is polled by the client.
Here’s an example: