First of all- I know I should use HTTP POST and I know how to do it.
But my task is to use HTTP GET anyway. Now, there's a problem. I have a WebMethod:
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json,UseHttpGet =true)]
public List<string> AddToCollection(string name, string lastname)
{
collection.Add(name + " " + lastname);
return collection;
}
Now, code from .js :
function AddToArray() {
var name = document.getElementById("name_add").value;
var lastname = document.getElementById("surname_add").value;
if (name == "" || lastname == "") {
alert("Wrong!");
} else {
var dataT = JSON.stringify({"name":name,"lastname":lastname});
$.ajax({
type: "GET",
url: "http://localhost:45250/ServiceJava.asmx/AddToCollection",
data: dataT,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//something
}
})
}
}
Now the thing is: it works perfect when I use type POST. But with GET I get the error that the parameter name cannot be recognized. What should I change to make it work?
Related
I want to create QR Code from user data. I have using below library for creating QR Code.
Install-Package QRCoder
Now I have done below code to convert data into base64 string.
private static Byte[] BitmapToBytes(Bitmap img)
{
using (MemoryStream stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return stream.ToArray();
}
}
public JsonResult SaveVisitorEntry(string FirstName, string LastName)
{
//VisitorEntryDA pdm = new VisitorEntryDA();
object obj = "";//pdm.getCity(country_id, Convert.ToInt64(state_id));
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(FirstName + " " + LastName, QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);
ImageConverter converter = new ImageConverter();
return Json(JsonConvert.SerializeObject(BitmapToBytes(qrCodeImage)));
}
Below is my AJAX code where i am converting Base64 to image and showing my QR code to image.
jQuery.ajax({
url: "../VisitorEntry/SaveVisitorEntry",
type: "POST",
//data: "{function:'" + value + "'}",
data: "{FirstName:'" + FirstName + "',LastName:'" + LastName + "'}",
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function (data) {
debugger
jQuery("#wait").css("display", "none");
toastr['success']('Line Stoppage closed Successfully');
//var results = JSON.parse(data);
//if (results.length == 0) {
// jQuery("#wait").css("display", "none");
//} else {
// f_group_data = results.Table;
// bindDropDown("function_name", f_group_data, "FUNCTION", "FUNCTION", "Select");
// jQuery('#function_name').chosen();
// jQuery('#function_name').trigger("chosen:updated");
// jQuery("#wait").css("display", "none");
//}
QrCodeImage.setAttribute('src', "data:image/jpg;base64," + data);
//setTimeout(function () {// wait for 5 secs(2)
// location.reload(); // then reload the page.(3)
//}, 3000);
},
error: function (data) {
jQuery("#wait").css("display", "none");
console.log("error");
}
});
I am not getting correct formate of base64 string and getting below error in console.
Failed to load resource: net::ERR_INVALID_URL
Below value I am getting in data variable
""iVBORw0KGgoAAAANSUhEUgAAAkQAAAJECAYAAAD34DtaAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAC5MSURBVHhe7dZRiiNLFgTRt/9Nz2zABGrMQbcy7YB9exAZ1a3//pckSfJy/SBKkiSv1w+iJEnyev0gSpIkr9cPoiRJ8nr9IEqSJK/XD6IkSfJ6/SBKkiSv1w+iJEnyev0gSpIkr9cPoiRJ8nr9IEqSJK/XD6IkSfJ6/SBKkiSv1w+iJEnyev0gSpIkr9cPoiRJ8nr9IEqSJK/XD6IkSfJ6/SBKkiSv1w+iJEnyev0gSpIkr9cPoiRJ8nr9IEqSJK/XD6IkSfJ6/SBKkiSv1w+iJEnyev0gSpIkr9cPoiRJ8nr9IEqSJK/XD6IkSfJ6/SBKkiSv1w+iJEnyev0gSpIkr9cPoiRJ8nr9IEqSJK/XD6IkSfJ6/SBKkiSv1w+iJEnyev0gSpIkr9cPoiRJ8nr9IEqSJK/XD6IkSfJ6/SBKkiSv1w+iJEnyev0gSpIkr9cPoiRJ8nr9IEqSJK/XD6IkSfJ6/SBKkiSv1w+iJEnyev0gSpIkr9cPoiRJ8nr9IEqSJK/XD6IkSfJ6/SBKkiSv1w+i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rhum3ELfyBSnG5ToUZpyC30j0xpt1O9aow1TbqFvZIrTDUr0KE25hb6RaY026net0YYpt9A3MsXpBiV6lKbcQt/ItEYb9bvWaMOUW+gbmeJ0gxI9SlNuoW9kWqON+l1rtGHKLfSNTHG6QYkepSm30DcyrdFG/a412jDlFvpGpjjdoESP0pRb6BuZ1mijftcabZhyC30jU5xuUKJHacot9I1Ma7RRv2uNNky5hb6RKU43KNGjNOUW+kamNdqo37VGG6bcQt/IFKcblOhRmnILfSPTGm3U71qjDVNuoW9kitMNSvQoTbmFvpFpjTbqd63Rhim30DcyxekGJXqUpjXaeHJrtGFao436XWu0YVqjjSe3RhumON2gRI/StEYbT26NNkxrtFG/a402TGu08eTWaMMUpxuU6FGa1mjjya3RhmmNNup3rdGGaY02ntwabZjidIMSPUrTGm08uTXaMK3RRv2uNdowrdHGk1ujDVOcblCiR2lao40nt0YbpjXaqN+1RhumNdp4cmu0YYrTDUr0KE1rtPHk1mjDtEYb9bvWaMO0RhtPbo02THG6QYkepWmNNp7cGm2Y1mijftcabZjWaOPJrdGGKU43KNGjNK3RxpNbow3TGm3U71qjDdMabTy5NdowxekGJXqUpjXaeHJrtGFao436XWu0YVqjjSe3RhumON2gRI/StEYbT26NNkxrtFG/a402TGu08eTWaMMUpxuU6FGa1mjjya3RhmmNNup3rdGGaY02ntwabZjidIMSPUrTGm08uTXaMK3RRv2uNdowrdHGk1ujDVOcblCiR2lao40nt0YbpjXaqN+1RhumNdp4cmu0YYrTDUr0KE1rtPHk1mjDtEYb9bvWaMO0RhtPbo02THG6QYkepWmNNp7cGm2Y1mijftcabZjWaOPJrdGGKU43KNGjNK3RxpNbow3TGm3U71qjDdMabTy5NdowxekGJXqUpjXaeHJrtGFao436XWu0YVqjjSe3RhumON2gRI/StEYbT26NNkxrtFG/a402TGu08eTWaMMUpxuU6FGa1mjjya3RhmmNNup3rdGGaY02ntwabZjidIMSPUrTGm08uTXaMK3RRv2uNdowrdHGk1ujDVOcblCiR2lao40nt0Yb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oDU/Iv6A2Zcktf5Bj6ozGt0cal1mjjUm9Dd2BK/gW9IVNu6YscQ380pjXauNQabVzqbegOTMm/oDdkyi19kWPoj8a0RhuXWqONS70N3YEp+Rf0hky5pS9yDP3RmNZo41JrtHGpt6E7MCX/gt6QKbf0RY6hPxrTGm1cao02LvU2dAem5F/QGzLllr7IMfRHY1qjjUut0cal3obuwJT8C3pDptzSFzmG/mhMa7RxqTXauNTb0B2Ykn9Bb8iUW/oix9AfjWmNNi61RhuXehu6A1PyL+gNmXJLX+QY+qMxrdHGpdZo41JvQ3dgSv4FvSFTbumLHEN/NKY12rjUGm1c6m3oDkzJv6A3ZMotfZHkD6N/ZE3X0ZlN19GZ6znllr5I8ofRP7Km6+jMpuvozPWccktfJPnD6B9Z03V0ZtN1dOZ6TrmlL5L8YfSPrOk6OrPpOjpzPafc0hdJ/jD6R9Z0HZ3ZdB2duZ5TbumLJH8Y/SNruo7ObLqOzlzPKbf0RZI/jP6RNV1HZzZdR2eu55Rb+iLJH0b/yJquozObrqMz13PKLX2R5A+jf2RN19GZTdfRmes55Za+SPKH0T+ypu…"
The base64 value you provided in your question is malformed, my recommendation is to not use Json Serialization for this Api response effort.
Try using Convert.ToBase64String
Ensure the method returns string
and the jQuery request accepts/expects text for dataType
response.
Diff in your Api Endpoint
public IActionResult SaveVisitorEntry(string FirstName, string LastName)
{
...
return Ok(Convert.ToBase64String(BitmapToBytes(qrCodeImage)));
}
Diff in your request
jQuery.ajax({
dataType: "text",
success: function (data) {
QrCodeImage.setAttribute('src', "data:image/jpg;base64," + data);
}
});
Edit
Correction on contentType, shoul be dataType that is changed to handle response of request.
Additionally,
Though I'm not certain this is related to the issue you're observing here, I do think the data request payload should be an object, not a string.
...
type: "POST",
data: {
FirstName: FirstName,
LastName: LastName
},
dataType: "text",
...
You should also consider handling scenarios where both Firstname and Lastname parameter are null, whitespace or string.Empty in your Api endpoint.
I would also string.Trim() here as a sanity effort: qrGenerator.CreateQrCode($"{FirstName} {LastName}".Trim(), QRCodeGenerator.ECCLevel.Q);
I am trying to send the variables from the GetTelephoneFunction to the AJAX query so it can reach the controller, but what I have tried does not work. Please help and thank you.
jQuery:
$.ajax({
type: "POST",
url: "/Status/GetUrlSource",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ "url": $("#urlInput").val(), "user": $("#user").val(), "pass":$("#pass").val(),"freq": $("#freqInput").val() }),
dataType: "html",
success: function (result, status, xhr) {
//Code
}
function GetUrlTelePhone(html, status, result) {
var geturlUser = url.split('?username=')[1].split('&password')[0];
var geturlPass = url.split("&password=")[1];
var getUrlMain = url.split("?")[0];
var geturlParameters = url.split("aspx")[1];
}
Controller
public string GetUrlSource(string url, string freq, string user, string pass)
{
//user = user.Substring(0, 10) != "?" ? "?" + user : user;
//pass = pass.Substring(0,20) != "&" ? "&" + pass : pass;
url = url.Substring(0, 4) != "http" ? "http://" + url : url;
string htmlCode = "";
using (WebClient client = new WebClient())
{
try
{
htmlCode = client.DownloadString(url);
}
catch (Exception)
{
return "Not Found";
}
}
//SqlConnect(url);
Hangfire(url,freq,user,pass);
return htmlCode;
}
I've been stuck on this problem for a good bit
I'm trying to add an object to my database through jQuery/AJAX. Apparently, there are no errors but it's not adding anything to my DB.
This is my JS/JQuery code:
var student = new Object();
student.Name = $("#txtNameAdd").val();
student.Age = $("#txtAgeAdd").val();
student.Email = $("#txtEmailAdd").val();
$.ajax({
type: "POST",
url: "Default.aspx/AddStudent",
data: "{'studentJSONString':'" + JSON.stringify(student) + "'}",
success: function (response) {
$("#lblSuccessAdd").text("Success!");
$("#lblSuccessAdd").css("display", "block");
},
error: function (response) {
alert(response.responseJSON);
}
});
My JS code points towards this code I have in my Default.aspx page code:
[WebMethod]
public static void AddStudent(string studentJSONString)
{
JavaScriptSerializer converter = new JavaScriptSerializer();
Student a = converter.Deserialize<Student>(studentJSONString);
WebMethods.AddStudent(a);
}
Which points to this code in my WebMethods.cs class
[WebMethod]
public static void AddStudent(Student student)
{
MasterStudent master = new MasterStudent();
master.AddStudent(student);
}
And finally that goes to my class library and finishes with this method in MasterStudent:
public void AddStudent(Student student)
{
if (string.IsNullOrWhiteSpace(student.Name))
{
throw new Exception("There's no name");
}
if (string.IsNullOrWhiteSpace(student.Email))
{
throw new Exception("There's no email");
}
using (studentEntities model = new studentEntities())
{
model.student.Add(student);
model.SaveChanges();
}
}
I run the code and the Console doesn't log any problems but it also doesn't do anything.
I have run very similar code on a Forms application with no problems so I'm kind of in a pickle right now. Does anyone know why it keeps failing?
Have you tried attaching a debugger to it and verifying that the student object is not null prior to model.SaveChanges() ?
Try debugging and verifying that the student string is being converted to an actual Student object first.
If it is, then try profiling the DB and validate any commands issued.
So I actually did find a solution shortly after posting my question, I changed my JQuery AJAX call to look like this:
$.ajax({
type: "POST",
url: "Default.aspx/AddStudent",
data: "{'studentJSONString':'" + JSON.stringify(student) + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#lblSuccessAdd").text("Success!");
$("#lblSuccessAdd").css("display", "block");
},
error: function (response) {
$("#lblErrorAdd").text(response.responseJSON.Message);
$("#lblErrorAdd").css("display", "block");
}
});
And it actually works now! so either the dataType or contentType were very important for what I was trying to do
Thanks for your answers everybody
var student = new Object();
student.Name = $("#txtNameAdd").val();
student.Age = $("#txtAgeAdd").val();
student.Email = $("#txtEmailAdd").val();
$.ajax({
type: "POST",
url: "Default.aspx/AddStudent",
data: student,
success: function (response) {
$("#lblSuccessAdd").text("Success!");
$("#lblSuccessAdd").css("display", "block");
},
error: function (response) {
alert(response.responseJSON);
}
});
Then you can to point to this WebMethod.
[WebMethod]
public static void AddStudent(Student student)
{
MasterStudent master = new MasterStudent();
master.AddStudent(student);
}
Try this.
could you please help with this. I have the following javascript:
$('form').click(function (e)
{
if (e.target.getAttribute('id') === 'SubmitAddLevel')
{
var parent = $('#' + e.target.getAttribute('attr')),
var Data = [];
parent.find('.input').children().each(function (i, e)
{
Data.push(e.getAttribute('id') + ":" + e.value);
console.log(Data);
});
$.ajax({
type: "POST",
url: 'AjaxControls.aspx/CreateUserLevel',
//data: Data, //.join(','),
dataType: "text",
contentType: "application/json; charset=utf-8",
//error: function (er) { alert(er); },
success: function (response)
{
if (response.d === "true")
{
$("#ErrorDivAddLevel").html('Level created successfully!').fadeIn('slow');
}
else
{
$("#SuccessDivAddLevel").html('Level creation failed!').fadeIn('slow');
}
},
});
}
The result of 'Data' I got on the console is :["LevelNameAddLevel:Admin", "PriviledgeIDAddLevels:|1|2|3|4|5|6|7|"]. How do I convert this to what ajax will pass to my web menthod?
Here is the web method
<WebMethod(EnableSession:=True)>
Public Shared Function CreateUserLevel(userLevel As String, userPriviledges As String) As String
return "true"
end function
I think your Data should look something more like this:
[{"LevelNameAddLevel":"Admin"}, {"PriviledgeIDAddLevels":"|1|2|3|4|5|6|7|"}]
So you have key / value pairs inside of an array. In the request, you should then be able to fetch the data via the keys in the request.
But I'm not quite sure what this is supposed to mean : "|1|2|3|4|5|6|7|"
I have a IEnumerable Collection of Custom DataType that I'm sending to the Client.
I want to parse the collection in my JQuery method. Currently I'm getting value as "undefined". Below is my code:
Service:
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
IEnumerable<CustomData> GetSetting(long userId);
public IEnumerable<CustomData> GetSetting(long userId)
{
var tempData = Context.DialogSettings.Where(item => item.id == userId).ToList();
return tempData.Select(dialogSetting => new CustomData { KeyName = dialogSetting.KeyName, KeyValue = dialogSetting.KeyValue }).ToList();
}
[DataContract]
public class CustomData
{
[DataMember]
public String KeyName;
[DataMember]
public String KeyValue;
}
Client:
function LoadSetting() {
$.ajax({
type: "GET",
url: "SampleService.svc/GetSetting",
data: '{"userId": "' + 1 + '"}',
processData: true,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var myHistoryList = data.d;
alert(myHistoryList); // here I'm getting value: undefined
},
error: function (result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
}
});
}
});
From the comments on the question I can safely presume that the following js code will work:
if(typeof data != 'undefined'){
alert(data[0].KeyName); //this will yield a value.
}
else
alert('Ok. This is weird');