I have a been working on a contact form I am new to ASP.net, I know an intermediate amount of C# I am working on a contact form. I want to send the Values as a json array and parse it with JSON.net, I tried every way I could think of to get it to work. Without success, I need to know how to properly send and receive JSON from an ASMX page. Is there an Example file or maybe a tutorial? Or can some one please tell me what I am doing wrong? This was the only way I could get it to read the post vars.
But Its just 2 arrays not key value pairs.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/jquery-2.0.3.min.js"></script>
</head>
<body>
<form action="#">
<input type="text" id="firstname" name="firstname" value=""/>
<input type="text" id="lastname" name="lastname" value=""/>
</form>
</body>
</html>
<script>
$(document).ready(function () {
var $serialize = $('form').serializeArray();
var stringify = JSON.stringify($serialize);
var keys = ['firstname', 'lastname'];
var list = [$('#firstname').val(), $('#lastname').val()];
var jsonText = JSON.stringify({ args: list, keys: keys });
$.ajax({
url: "validation.asmx/sendRequest",
method: "POST",
dataType: "json",
data:jsonText,
cache: false,
processData: true,
contentType: "application/json; charset=utf-8"
}).done(function (data) {
console.log(data);
});
});
</script>
Here is my asmx file,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Newtonsoft.Json;
using System.Web.Script.Services;
using System.Data;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class validation : System.Web.Services.WebService {
public validation () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string sendRequest(List<string> args, List<string> keys)
{
var arg = args;
var key = keys;
return args[0];
}
}
here is my web config file
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>