Answer the question
In order to leave comments, you need to log in
How to make Newtonsoft.Json return pure json?
There is a C# service that gives a json string to a js script. I am using the Newtonsoft.Json library. But in my implementation, the service wraps the json in the <string></string> tag, which, accordingly, prevents me from working with it normally on the client. How can you get rid of it?
JS code:
function VerifySignature() {
var keyInfo = GetKeyInfo();
// отправляем данные в контроллер для валидации сертификата
$.ajax({
type: "POST",
url: "VerifySignatureService.asmx/VerifySignature",
data: { certInfo: JSON.stringify(keyInfo) },
success: function (result) {
alert(result);
console.log(result);
}
});
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class VerifySignatureService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string VerifySignature(string certInfo)
{
ResponseType response = new ResponseType();
if (string.IsNullOrEmpty(certInfo) || certInfo == "[]")
{
response.isError = true;
response.isValid = false;
response.message = "Пришел пустой keyInfo";
return JsonConvert.SerializeObject(response);
}
var keyInfo = JsonConvert.DeserializeObject<KeyInfo>(certInfo);
if (CertificateVerifier.VerifyCertificateSign(true, keyInfo))
{
response.isError = false;
response.isValid = true;
response.message = Resources.SuccessfullySigned;
}
else
{
response.isError = false;
response.isValid = false;
response.message = "Ваш ключ не прошел проверку";
}
return JsonConvert.SerializeObject(response, Formatting.Indented);
}
}
public class ResponseType
{
public string message;
public bool isError;
public bool isValid;
}
<string xmlns="http://tempuri.org/">{ "message": "Документ успешно подписан", "isError": false, "isValid": true }</string>
Answer the question
In order to leave comments, you need to log in
Newtonsoft.Json has nothing to do with it. The point is in the request, from the client side. You need to specify the application/json content type and pass the JSON to data :
$.ajax({
type: "POST",
url: "WebService1.asmx/VerifySignature",
contentType: "application/json",
data: JSON.stringify({ certInfo: keyInfo }), // обернуть все параметры
success: function (result) {
alert(result);
console.log(result);
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question