私はお問い合わせフォームに取り組んでいます。ASP.net は初めてです。お問い合わせフォームに取り組んでいる C# の中間量を知っています。値をjson配列として送信し、JSON.netで解析したいので、考えられるあらゆる方法を試して動作させました。成功しない場合は、ASMX ページから JSON を適切に送受信する方法を知る必要があります。サンプルファイルやチュートリアルはありますか? または、誰かが私が間違っていることを教えてもらえますか? これが、投稿変数を読み取る唯一の方法でした。
しかし、キーと値のペアではなく、2 つの配列だけです。
<!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>
これが私のasmxファイルです。
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];
}
}
ここに私のWeb設定ファイルがあります
<?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>