WCF works ONLY Asynchronously - asp.net

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
});

Related

Parameters lost zeros in Ajax request

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.

Ajax with asp.net aspx returns Undefined

I'm facing the following issue: Whenever I click on the button, the alert shows undefined.
Web method:
[WebMethod]
public static string getTest()
{
return "testing success";
}
Ajax script
<script type="text/javascript">
function getTest() {
$.ajax({
type: "POST",
url: "main.aspx/getTest",
data: "{}",
datatype: "json",
contenttype: "/application/json; charset=utf-8",
success: function (msg) {
alert(msg.d);
},
error: function (data) {
}
});
}
</script>
datatype should be dataType and contenttype should be contentType. Also remove / from start of "/application/json; charset=utf-8"
$.ajax({
type: "POST",
url: "main.aspx/getTest",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (msg) {
alert(msg.d);
},
error: function (data) {
}
});
Remove these from your ajax call
datatype: "json",
contenttype: "/application/json; charset=utf-8",
More about these
Differences between contentType and dataType in jQuery ajax function
What is content-type and datatype in an AJAX request?
You can find more details in the jQuery Ajax documentation

Microsoft AJAX vs jQuery AJAX

When calling an ASMX service, what benefits (if any) does this:
ProjectName.Services.ServiceName.MethodName('methodParameter', function (result) {
// do something with result
});
have over this:
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
//
},
error: function(e){
//
}
});
(Please note I'm not interested in "just use WCF, ASMX is outdated", it's purely for AJAX so I'm happy to stick with it for now.)

calling asmx service using jquery ajax asp.net 4.0

I'm trying to call a sample asmx service using jquery, here is the jquery code
$.ajax({
type: "POST",
url: "/Services/Tasks.asmx/HelloWorld",
data: "{}",
dataType: "json",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);
}
});
This is not showing any message,code is in asp.net 4.0,
Am I missing any thing?
Edit - I changed the dataType to xml, now success function is working it return following xml
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>
I'm using following code to parse xml data and it is showing null in alert
success: function (data) {
edata = $(data).find("string").html();
alert(data);
}
I believe it's because you have the dataType: "json" and it's expecting the response content-type to be the same but XML is being returned. I bet the complete event is being raised but not success.
try
$.ajax({
type: "POST",
url: "/Services/Tasks.asmx/HelloWorld",
data: "{}",
dataType: "json",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);
},
complete: function (data) {
alert(data);
}
});
UPDATE
I think it's because you're using .html(), you need to use text(). Also i don't know if you meant to do it or not but you have data in your alert, i'm assuming you meant to use edata. The following worked for me:
jQuery.ajax({
type: "POST",
url: "/yourURL",
dataType: "xml",
data: "{}",
contentType: "application/xml; charset=utf-8",
success: function(data) {
edata = $(data).find("string").text();
alert(edata);
}
})
I'd recommend adding the [ScriptService] attribute to your Tasks.asmx class so it will accept and respond in JSON instead of XML. Your client code looks good, but you'll want to take a look at "data.d" instead of "data" in your success handler.
use it.
<script>
alert("aaa");
$.ajax({
type: "POST",
url: "MyService.asmx/HelloWorld",
data: "{}",
dataType: "xml",
contentType: "application/xml; charset=utf-8",
success: function (data) {
alert(data);//data-object xmldocument
edata = $(data).children("string").text();
alert(edata);
}
});
alert("bbb");
</script>
Well, you're stating that the dataType is JSON, but the contentType is XML. Try
contentType: "application/json; charset=utf-8",
If not, then we'd have to see the asmx code.

How to pass int values to ASP.NET page methods from jQuery?

I am using ASP.NET page methods with jQuery. Here is my code,
$.ajax({
type: "POST",
url: "Default.aspx/GetRecords",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
and the ASP.NET page method is,
[WebMethod]
public static string GetRecords(int currentPage,int pagesize)
{
// my logic here
}
How to pass values for currentPage and pagesize from jQuery?
I got it working. My data section must be
data: "{'currentPage':1,'pagesize':5}",
Did you try just:
$.ajax({
type: "POST",
url: "Default.aspx/GetRecords",
data: {"currentPage": 1, "pageSize": 100},
contentType: "application/json; charset=utf-8",
dataType: "json",
?
Try this
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{ currentPage: '" +parseInt( $('#currentpage').val()) + "',pageSize:'"+parseInt( $('#pagesize').val()) +"'}",
url: "Default.aspx/GetRecords",
dataType: "json",
success: function (data) {
$("#result").html(data.d);
}
});

Resources