deserialize object when ajax call asmx web service - javascript

I have AJAX call where call web service. web service is should return value (token as a string). I tried my web service and try to invoke manually, it successfully retrieve the token as string. but, when I combine with AJAX call, it returns an object. How I can retrieve the value of the token?
here is my AJAX snippet
$.ajax({
url: "http://10.23.64.43:8035/iFrameIntegration.asmx/getToken",
data: JSON.stringify({ Token: { userName: 'crm' } }),
contentType: "application/json; charset=utf-8",
type: 'POST',
success: function (data) {
alert(data);
},
error: function (data) {
alert("Error");
}
});
and here is my web service (ASMX)
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getToken()
{
var request = (HttpWebRequest)WebRequest.Create("http://10.23.64.37:8080/ACCMWS/member/SSOInit");
request.Method = "POST";
request.ContentType = "application/json";
request.Headers["userId"] = "Svc_CRM";
request.Headers["loginType"] = "Internal";
request.Headers["token"] = "54a93982adf51adfb81885ddbbb1874e271605ce";
string result = string.Empty;
try
{
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
string json = "{ \"userName\": \"crm\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
try
{
using (var response = request.GetResponse() as HttpWebResponse)
{
if (request.HaveResponse && response != null)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
var resultTokenAwal = reader.ReadToEnd();
var resultToken = JsonConvert.DeserializeObject<RetrieveSSOTokenResult>(resultTokenAwal);
result = resultToken.data.ssotokenList[0].ssoToken;
}
}
}
}
catch (WebException e)
{
if (e.Response != null)
{
using (var errorResponse = (HttpWebResponse)e.Response)
{
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
{
string error = reader.ReadToEnd();
result = error;
}
}
}
}
}
catch(Exception ex)
{
ex.Message.ToString();
}
return result;
}
and here is my class from my web service
public class RetrieveSSOTokenResult
{
public string status { get; set; }
public string message { get; set; }
public Data data { get; set; }
}
public class Data
{
public Ssotokenlist[] ssotokenList { get; set; }
}
public class Ssotokenlist
{
public string loginType { get; set; }
public string userName { get; set; }
public string userId { get; set; }
public string ssoToken { get; set; }
public long expiryTime { get; set; }
}
here is the result that I get when call from AJAX
{"d":"f0d7ef7e29b89d42e84d6590b8e7b52f899f3b7a"}
please advise, I only want to alert the value of the token, which is f0d7ef7e29b89d42e84d6590b8e7b52f899f3b7a.
in the asmx, when I try to invoke, I only got this information
var from resultTokenAwal is this
{"d":"status":"success","message":"","data":{"ssotokenList":[{"loginType":"Internal","userName":"CRM","userId":"E512E6D2-1584-4597-BBD6-01C724496107","ssoToken":"2f3b28def21bdace7bd7baacd0cd0bf72fbcb30b","expiryTime":1548047778532}]}}
but, in my asmx, the result of resultTokenAwal is this
{"status":"success","message":"","data":{"ssotokenList":[{"loginType":"Internal","userName":"CRM","userId":"E512E6D2-1584-4597-BBD6-01C724496107","ssoToken":"2f3b28def21bdace7bd7baacd0cd0bf72fbcb30b","expiryTime":1548047778532}]}}
UPDATE:
the data actually already parse in JSON in AJAX. so, just call the d. here is the update of AJAX.
$.ajax({
url: "http://10.23.64.43:8035/iFrameIntegration.asmx/getToken",
data: JSON.stringify({ Token: { userName: 'crm' } }),
contentType: "application/json; charset=utf-8",
type: 'POST',
success: function (data) {
alert(data.d);
},
error: function (data) {
alert("Error");
}
});

Related

Posting to mvc controller action always return error

I am new to mvc and I am tring to use ajax to get and post data. Get is working fine but when I use post using the same ajax code while change it to post give me always error 400. I tried with many type of data sent and the action in the controller is empty to eleminate that the error occour there although i new 400 error is a bad request but I couldn't figure it out.
The code is as follow
async function SaveTheDay(url, data) {
if (data != null) {
$.ajax({
url: url,
type: 'POST',
dataType: "json",
data: data,
success: function (response) {
if (response) {
console.log("Ok");
} else {
console.log("not valid ");
}
},
error: function (req, status, error) {
console.log("error "+status+" "+error);
}
})//not null
}
}//SaveTheDay
the controller action is :
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SaveDay(InfoDay day)
{
return View("DayTrial");
}
and I call the function when the save button cliked as:
//define the object
var infoDay =
{
userId :userId,
date: TheDate.value ,
items :items ,
itemsAmount : itemsAmount,
itemsMealTime :itemsMealTime,
dailyConsumedkCal: dailyConsumedkCal,
dayTotal :"",
bloodData :"",
bloodSugarData :"",
sleepData :"",
indexData :"",
dayCost :0
}
SaveTheDay("/Days/SaveDay", infoDay);
here is the InfoDay class
public class InfoDay
{
public int userId { get;
set; }
public string date { get; set;
}
public string? items { get;
set; }
public string? itemsAmount {
get; set; }
public string? itemsMealTime {
get; set; }
public int? dailyConsumedkCal
{ get; set; }
public string? dayTotal {
get; set; }
public string? bloodData {
get; set; }
public string? bloodSugarData
{ get; set; }
public string? sleepData {
get; set; }
public string? indexData {
get; set; }
public int? dayCost { get;
set; } = 0;
}
The ValidateAntiForgeryToken attribute prevents 'Cross-site request forgery' attacks in your MVC application.
See https://learn.microsoft.com/..../security/anti-request-forgery?view=aspnetcore
Do not remove it.
To avoid the 400 error, you must pass the token with your POST request.
async function SaveTheDay(url, data) {
if (data != null) {
$.ajax({
url: url,
type: 'POST',
dataType: "json",
data: data,
beforeSend: function (xhr) {
xhr.setRequestHeader('RequestVerificationToken',
$('input:hidden[name="__RequestVerificationToken"]').val());
},
success: function (response) {
if (response) {
console.log("Ok");
} else {
console.log("not valid ");
}
},
error: function (req, status, error) {
console.log("error "+status+" "+error);
}
})//not null
}
}//SaveTheDay
Of course the hidden field with the token has to be present in the view.
The HTML Helper Html.AntiForgeryToken includes the token.
#Html.AntiForgeryToken()
If you use a form tag helper, the token is also included.
See https://learn.microsoft.com...?view=aspnetcore-6.0#the-form-tag-helper

.Net core - Ajax post does not pass variables to controller method

When I set a breakpoint on LoadReport, every parameter is null. For some reason the values are not binding to the parameters with the same name.
Javascript/AJAX
$('#savedCriteria').on('change', function () {
var criteriaSelected = $('#savedCriteria option:selected').text();
var data = { actionName: "Daily", reportInput: "ReportDaily", reportCriteria: criteriaSelected };
//Ajax form post
$.ajax({
type: 'POST',
data: data,
contentType: "application/json; charset=utf-8",
url: '#Url.Action("LoadReport", ViewContext.RouteData.Values["Controller"].ToString())',
success: function (data) {
if (data.success) {
alert("Test");
} else {
alert("Test Not Successful");
}
}
});
});
Controller
public void LoadReport(string actionName, string reportInput, string reportCriteria)
{
var reportObject = Activator.CreateInstance(Type.GetType(reportInput));
IEnumerable<Test.Reports.Utilities.ReportCriteria> reportList = getReportCriteria(reportInput);
RedirectToAction(actionName, "Reports", reportList.Where(x => x.CriteriaName == reportCriteria));
}
Default method type is HttpGet, you need to set it to HttpPost.
[HttpPost]
public void LoadReport(string actionName, string reportInput, string reportCriteria)
{
var reportObject = Activator.CreateInstance(Type.GetType(reportInput));
IEnumerable<Test.Reports.Utilities.ReportCriteria> reportList = getReportCriteria(reportInput);
RedirectToAction(actionName, "Reports", reportList.Where(x => x.CriteriaName == reportCriteria));
}
Also keep in mind that with your ajax call you can not use RedirectToAction. You need something like this:
[HttpPost]
public ActionResult LoadReport(string actionName, string reportInput, string reportCriteria)
{
var reportObject = Activator.CreateInstance(Type.GetType(reportInput));
IEnumerable<Test.Reports.Utilities.ReportCriteria> reportList = getReportCriteria(reportInput);
Return Json(Url.Action(actionName, "Reports", reportList.Where(x => x.CriteriaName == reportCriteria));
}
And in your ajax call:
success: function (data) {
window.location.href = data;
}
UPDATE: you also need to create a POCO object and add that to the HttpPost method as parameter instead of separate parameters. Also [FromBody] attribute is needed.
POCO:
public class Data
{
public string actionName { get; set; }
public string reportInput { get; set; }
public string reportCriteria { get; set; }
}
Controller:
[HttpPost]
public JsonResult LoadReport([FromBody]Data data)
{
var reportObject = Activator.CreateInstance(Type.GetType(data.reportInput));
IEnumerable<Test.Reports.Utilities.ReportCriteria> reportList = getReportCriteria(data.reportInput);
return Json(Url.Action(data.actionName, "Reports"));
}

Posting JSON Array Collection to Asp.net Core Web API

Need your expertises
I am working with JS & Asp.net Core Web API.
I am trying to post a JSON Array to my API but i am failing to get data in web api post method it is NULL every time need help in understanding what is that I am missing..
Below is JS Code
function Trails(){
var A = [];
var B = [];
var MyData = [];
for(var i = 0;i <= 2;i++){
var _A = {
"ID" : i,
"_A" : "A"+i
};
A.push(_A);
}
for(var i = 0;i <= 2;i++){
var _B = {
"ID" : i,
"_B" : "B"+i
};
B.push(_B);
}
for(var i = 0;i <= 2;i++){
MyData.push({
"MyDataName" : "CJ"+i,
"A" : A,
"B" : B,
});
}
$.ajax({
crossDomain: true,
cache: false,
type: "POST",
url: "https://localhost:44359/api/values/",
contentType: "application/json",
data :JSON.stringify({MyData}),
dataType: "json",
success: function (data) {
alert(data);
}, //End of AJAX Success function
failure: function (data) {
alert(data.responseText);
console.log(data);
}, //End of AJAX failure function
error: function (data) {
alert(data.responseText);
console.log(data);
} //End of AJAX error function
});
}
C# Classes
public class A
{
public int ID { get; set; }
public string _A { get; set; }
}
public class B
{
public int ID { get; set; }
public string _B { get; set; }
}
public class MyData
{
public string MyDataName { get; set; }
public List<A> A { get; set; }
public List<B> B { get; set; }
}
Web API Post method
// POST api/values
[HttpPost]
public void Post([FromBody] List<MyData> MyData)
{
}
MyData is always null. please help
You are using: JSON.stringify({MyData}) instead of JSON.stringify(MyData). It goes to the server as a JSON containing field "MyData" with MyData, instead of just MyData array.

Passing an object with values from View to ActionResult using ajax

I want to send an object from view to controller, ajax hits the action result but data is not being delivered to action result
View:
function submit()
{
var obj = {};
obj.PD = getResult('pd');
obj.MD = getResult('md');
obj.C = getResult('c');
obj.ME = getResult('me');
obj.EE = getResult('ee');
obj.SED = getResult('sed');
obj.RT = getResult('rt');
obj.SEA = getResult('sea');
$.ajax({
url: '/Assessment/AssessNow',
type: 'POST',
async: false,
data: '{obj' + JSON.stringify(obj) + '}',
dataType: 'json',
success: function (res) {
},
error: function (msg) {
}
});
//alert(getResult('pd'));
}
Model:
public class QAViewModel
{
public string C { get; set; }
public string EE { get; set; }
public string MD { get; set; }
public string ME { get; set; }
public string PD { get; set; }
public string RT { get; set; }
public string SEA { get; set; }
public string SED { get; set; }
}
Controller:
Editing as a good point was raised:
In the post you can just pass the full object like so:
function submit()
{
var obj = {};
obj.PD = getResult('pd');
obj.MD = getResult('md');
obj.C = getResult('c');
obj.ME = getResult('me');
obj.EE = getResult('ee');
obj.SED = getResult('sed');
obj.RT = getResult('rt');
obj.SEA = getResult('sea');
$.ajax({
url: '/Assessment/AssessNow',
type: 'POST',
async: false,
data: obj,
success: function (res) {
},
error: function (msg) {
}
});
//alert(getResult('pd'));
}
If you want to stick with json then modify your ajax call accordingly (you error was in the way you were building the data property:
$.ajax({
url: '/Assessment/AssessNow',
type: 'POST',
async: false,
data: JSON.stringify({obj: obj}),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (res) {
},
error: function (msg) {
}
});
Also, depending on what you are doing with the result you may need to flip your controller actions to return JsonResult (Ignore if you are doing something such as returning a partial view to load):
[HttpPost]
public JsonResult Whatever(QAViewModel obj)
{
return Json(whatever, JsonRequestBehavior.AllowGet);
}

Controller recieving empty data from Ajax

I'm trying to send a collection from a view to a controller, using ajax. First I complete the data in a javascript array, and then I try to pass it to the server:
var funciones = $("#funcionesUsuario").data("kendoGrid").dataSource.data();
var func = [];
for (var i = 0; i < funciones.length; i++) {
func.push({
"Estado": 1,
"FechaAlta": null,
"UsuarioAlta": null,
"FechaModificacion": null,
"UsuarioModificacion": null,
"BitConcilia": funciones[i].BitConcilia,
"BitLectura": funciones[i].BitLectura,
"BitSupervisa": funciones[i].BitSupervisa,
"ConciliacionId": funciones[i].ConciliacionId,
"UsuarioId": funciones[i].UsuarioId
})
}
$.post(url, { funcionesUsuario: func })
.done(function (data) {
alert("good job");
});
Then, since I'm sending data for 2 objects, my parameter is a IEnumerable of said object:
public void ActualizarFuncionesUsuario(IEnumerable<FuncionUsuario> funcionesUsuario)
{
//do something
}
My problem is that the controller receives 2 objects, as I can see in the funcionesUsuario.count, but they are both empty.
I tried sending int, bool and other types of variables with success, so I suspect I'm doing something wrong regarding data binding.
Below I attached pictures of what I'm sending and what I'm receiving in the other side.
This is the FuncionUsuario model: `
public class FuncionUsuario : AuditableEntity {
public int ConciliacionId { get; set; }
public int UsuarioId { get; set; }
public bool BitLectura { get; set; }
public bool BitConcilia { get; set; }
public bool BitSupervisa { get; set; }
public virtual Conciliacion Conciliacion { get; set; }
[Display(ResourceType = typeof(Global), Name = "Descripcion")]
[NotMapped]
public string Descripcion { get; set; }
}
first construct you array as java script objects
var funciones = $("#funcionesUsuario").data("kendoGrid").dataSource.data();
var func = [];
for (var i = 0; i < funciones.length; i++) {
func.push({
Estado: 1,
FechaAlta: null,
UsuarioAlta: null,
FechaModificacion: null,
UsuarioModificacion: null,
BitConcilia: funciones[i].BitConcilia,
BitLectura: funciones[i].BitLectura,
BitSupervisa: funciones[i].BitSupervisa,
ConciliacionId: funciones[i].ConciliacionId,
UsuarioId: funciones[i].UsuarioId
})
}
Next, properly stringify them
var payload = JSON.stringify(func);
Then post it as JSON
$.ajax({
url: url,
type: "POST",
contentType: "application/json",
dataType: 'json'
data: payload,
});
Try these:
Specify json as the dataType
$.post(url, { funcionesUsuario: func })
.done(function (data) {
alert("good job");
}, 'json');
Use $.ajax instead: docs
$.ajax({
url: url,
data: JSON.stringify(func),
contentType: "application/json",
type: "POST"
});

Categories

Resources