Parameters lost zeros in Ajax request - asp.net

I send parameters for WebMethod with Ajax and receive um object. But, paramters lost zeros in WebMethod, i send "00001234", in backend paramenters is "1234"
Ajax Code:
var content = {valueS: "00001234" };
$.ajax({
type: "GET",
dataType: 'json',
data: content,
async: false,
url: "myPage.aspx/GetData",
contentType: "application/json; charset=utf-8",
success: function (data) { OnSucess(data); },
error: function(data) { OnError(data); }
});
Webmethod Code:
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)]
public static string GetData(string valueS)
{...}

Try
var content = {valueS: "'00001234'" };
$.ajax({
type: "GET",
dataType: 'json',
data: content,
async: false,
url: "myPage.aspx/GetData",
contentType: "application/json; charset=utf-8",
success: function (data) { OnSucess(data); },
error: function(data) { OnError(data); }
});
seems valueS is being deserialized to a number first, in which case it will loose its leading zeros.
The request generated by your code looks like this myPage.aspx/GetData?valueS=00001234
while the one generated by my code looks like this myPage.aspx/GetData?valueS=%2700001234%27, hence will be correctly deserialized as a string.

Related

How pass id from client to webform using ajax

My code doesn't work, I do not know why.
My ajax code call:
var id = $(this).attr("data-id")
$.ajax({
type: "POST",
url: "XTM307.aspx/GetPart",
data: { "id": id },
contentType: "application/json",
dataType: "json",
success: function (response) {
}
});
My server code:
<System.Web.Services.WebMethod()>
<ScriptMethod(UseHttpGet:=True, ResponseFormat:=ResponseFormat.Json)>
Public Shared Sub GetPart(id As String)
Dim test = 2
test += 3
End Sub
Your webservice method is GET, so the ajax call should be like:
var id = $(this).attr("data-id")
$.ajax({
type: "GET",
url: "XTM307.aspx/GetPart?id=" + id,
contentType: "application/json",
dataType: "json",
success: function (response) {
}
});

WCF works ONLY Asynchronously

I have a WCF “MWManageSession” service inside my WebApplication so I don’t have any service reference.
The problem is that seems to work only asynchronously instead of synchronously.
public interface IMWManageSession{
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json)]
string SetIdSelezionato_SessionData(String[] pvalori, SessionNavigation pSN, long varpChangingAzienda);
}
I consume the wcf on a client function using
$.ajax({
type: "POST",
url: webMethod,
data: jsonText,
processData: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
ServiceSucceeded(msg, ptoPopup, DatixWCF, myvar);
},
error: ServiceFailed
});
function ServiceSucceeded(result, ptoPopup, DatixWCF, myvar) {
console.log("ServiceSucceeded: " + result);
}
I get execute code that I put on “OnServiceSucceed” while the wcf is steel working…
How can I make the wcf works ONLY synchronously?
Thanks in advance !
Maybe you can try to add to your ajax post async: false
Something like this:
$.ajax({
type: "POST",
url: webMethod,
data: jsonText,
processData: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (msg) {
ServiceSucceeded(msg, ptoPopup, DatixWCF, myvar);
},
error: ServiceFailed
});

ASP.NET hello world AJAX post

I keep getting a 500 with the following C#/jQuery. Any given implementation may not be right and thats not a huge issue. I'm just trying to get a hello world up. It works if the c# has no arguments but as soon as I try to recieve data it gives the 500.
[WebMethod]
public static string Test(string s)
{
// never gets here
}
$.ajax({
type: "POST",
url: "ajax.aspx/" + method,
/*async: true,*/
data: "{data:'" + data + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
callback(data.d);
}
});
latest attempt is this which still doesnt work:
[WebMethod()]
public static string Test(string data)
{
// never gets here
return "hello world";
}
$.ajax({
type: "POST",
url: "ajax.aspx/Test",
data: "data: {data:'abc'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("back");
}
});
I think you don't have to use MVC to make it work. I think the way you are passing json parameters are wrong. Please check below code and try and do let me know whether it works.
[WebMethod()]
public static string Test(string data)
{
// never gets here
return "hello world";
}
$.ajax({
type: "POST",
url: "ajax.aspx/Test",
data:'{"data":"abc"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});
try this
[HttpPost]
public ActionResult Test(string x)
{
// never gets here
return Json(true)
}
$.ajax({
type: "Post",
url: "ajax/Test",
data: {x:'abc'},
dataType: "json",
success: function (data) {
alert("back");
}
});

asp.net page method with jquery and parameter

In my javascript, I have:
var testdate = "{'TheNewDate' : '12/02/2011'}";
$("#mydiv").click(function () {
$.ajax({
type: "POST",
url: "../Pages/Appointments.aspx/GetAppointements",
data: testdate,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFn,
error: errorFn
});
});
In my code behind I have
[WebMethod]
public static string GetAppointements(string DateInput)
{
var t = DateInput;
However, when I click to run the call, I get the error function to activate. When I change the code behind function to public static string GetAppointement() it works. But I guess my goal is to pass a parameter to the code behind. What am I missing?
Thanks.
Your parameter is called DateInput and not TheNewDate, so:
$('#mydiv').click(function () {
$.ajax({
type: 'POST',
url: '../Pages/Appointments.aspx/GetAppointements',
data: JSON.stringify({ dateInput: '12/02/2011' }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: successFn,
error: errorFn
});
});
You should make your JSON data match the parameter name in the web service method.
var testdate = "{'DateInput' : '12/02/2011'}";

Calling asp.net webmethod with params from jquery errors

I managed to setup a simple webmethod which i called from jquery and sure enough it returns ... then i added parameters on the method and added the params to jquery but it errors with
Message":"Invalid JSON primitive: one.","StackTrace":"
my signature on my webmethod is like so
[WebMethod]
public static string GetDate(string one, string two)
{
return "yes";
}
and my jquery is like this, what am i doing wrong?
$.ajax({
type: "POST",
url: "MyService.aspx/GetDate",
data: { one: "value", two: "value" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg.d);
},
error: function(msg) {
alert('error');
}
});
Try enclosing your data parameter in quotes:
data: '{ one: "value", two: "value" }',

Resources