Consume ASP .Net WebService using HTML AJAX - c#

I am trying to call a simple hellowworld function of ASP .NET WebService using JQuery AJAX call written in separate HTML file i.e. Not aspx file in ASP .NET.
Here is code of my AJAX call which works fine in aspx based web client.
$.ajax({
type: "POST",
url: "/Test1/RSSReader.asmx/GetRSSReader",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#output").text(msg.d);
},
error: function (e) {
$("#output").html("WebSerivce error " + e.responseText);
}
});
I want to call same webservice and same method using .html file but it is returning error. I tried giving server address i.e. localhost but still no response.

Related

Failed to load resource: net:: ERR_CONNECTION_REST

I need to get large data from database using Ajax web call.
Upto 20000 records the ajax call working fine, But when the record count is more than 20k it showing the error
"Failed to load resource: net:: ERR_CONNECTION_REST"
in console.
There is no error in DL method.
And here is the ajax call code:
$.ajax({
type: "POST",
url: serviceUrl,
async: false,
data: JSON.stringify(Data);,
contentType: "application/json", // content type sent to server
dataType: "json", //Expected data format from server
processdata: true,
success: function (data) {//On Successfull service call
bSuccess = true;
//Success code here
},
error: function (jqXHR, textStatus, errorThrown) {
//Error code here
}
});
please help me to clear the error.
How large is each record?
My first guess would be the size of the request. What kind of server are you using? If it's hosted in IIS you should check the maxAllowedContentLength in web.config.

how to handle multiple jqgrid instances on same page with ashx file ASP.NET

I'm using Webform and Jqgrid to display master-detail information on same page.
Here is my Jqgrid definition:
Master JQGrid:
$("#MachineListGrid").jqGrid({
url: 'AdminHandler.ashx',
datatype: "json",
...
});
Detail JQGrid:
$("#MachineDetailListGrid").jqGrid({
url:'AdminHandler.ashx',
datatype: "json",
...
});
my question is, how does ashx file identify data to return json data back to the correct jqgrid?
I'd looked at the same between aspx and ashx from this tutorial but the tutorial only gave sample one JQGrid on page.
on the code, here is the way to capture the request:
System.Collections.Specialized.NameValueCollection forms = context.Request.Form;
The way that will solve the problem is to have two url - one for the master and another for the detail
$("#MachineListGrid").jqGrid({
url: 'AdminHandlerMaster.ashx',
datatype: "json",
...
});
$("#MachineDetailListGrid").jqGrid({
url:'AdminHandlerDatil.ashx',
datatype: "json",
...
});
If you can't do this you may identify it with additional parameter in the post data something like this
$("#MachineListGrid").jqGrid({
url: 'AdminHandler.ashx',
datatype: "json",
postData : { gridtype:"master"},
...
});
$("#MachineDetailListGrid").jqGrid({
url:'AdminHandler.ashx',
datatype: "json",
postData : { gridtype:"detail"},
...
});
In the response you will need to get the gridtype parameter to identify the master and detail data

Jquery Ajax call to ASMX web service error

I have a ASP.NET Web Forms application that has an ASMX web service file with a single method at the moment. I am trying to call that method from another file in the same project, but it does not seem to be working. When I look at my console window in chrome it states there is an internal 500 error with the web service. using the ASMX interface I am able to manually invoke the method, but it doesn't invoke from my AJAX code. I have been unable to determine the source of my error.
ASMX Web Method
[WebMethod]
public string TestMethod()
{
string x = "{Name:Shooter McGavin}";
return x;
}
JQuery from ASP.NET Page. I know the click event is firing and I also confirmed that the code is progressing inside the IF statement if the form is valid.
$("#AddSupplierBtn").click(function()
{
if ($("#AddSupplier").valid())
{
$.ajax({
type: "POST",
url: "/StockPileDelivery.asmx/TestMethod",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.data);
}
});
}
});

WebMethod called through Jquery Ajax is doing 301 redirect

I have a webmethod in aspx page and I am calling it through jquery Ajax method.
In one server I am getting windows security prompt on ajax call (all other servers are working fine).
When I was checking using fiddler I see a 301 redirect of my method call(webmethods.aspx/GetDetails to webmethods.aspx/GetDetails/)
Not sure why the redirect is happening on one server and call to webmethod.aspx/GetDetails/ is throwing 401.
I checked all the wildcard mapping etc and not able to find any issues. Any idea where else I need to check?
Here is my code
$.ajax({
type: "POST",
url: "/webmethods.aspx/GetDetails",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
//alert('success');
},
failure: function (response) {
alert(response);
},
error: function (jqXHR, textStatus, errorThrown) {
var errMessage = "An error occured serving your request. Please try again.";
if (jqXHR)
errMessage = $.parseJSON(jqXHR.responseText).Message;
alert(errMessage);
}
Can you check the handler order in your web.config? I have had similar issue where staticfile handler was before aspx handler. Changing the order fixed my issue (move the static file as the last element, because most of the time it will have verify whether file exists before handling).

unable to render Json string on client side

ascx.cs
protected string BindData()
{
List<Product> products = product.GetRepeaterData(prod);
string json = JsonConvert.SerializeObject(products);
return json;
}
ascx
<script type="text/javascript" language="javascript">
function doSomething() {
$.ajax({
type: "POST",
url: "/ProgramListSimple.aspx/BindData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg);
}
});
};
I am not able to see any alert ..I dont know if the ajax function is doing what it is suppose to do..this code is for user control & not on the aspx page does that matter? while debugging I am able to see the serialized data in json string. Its just that its not rendering on the client side....working on it since morning now I need some help please..any examples or any doc can also be useful..
You have to use the d property
alert(msg.d);
If you are using Chrome or Firefox to debug use the following to inspect a JavaScript object:
console.log("%o", msg);
In Chrome press Ctrl + Shift + J to show the developer console
I created a new aspx page. Transfered all the code behind logic to the aspx.cs from ascx.cs. The just called the url of the aspx page from my ascx page using ajax callback
type: "GET",
url:'<%=VirtualPathUtility.ToAbsolute("~/ProgramListSimpledetail.aspx") %>',
data: dataObject,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data)
I just added a small part of the fix that is very important for this to work other then this there were bunch of things that were added to make the ascx page inherit the properties from the apsx page.... but I think that was mainly related to my code...so I hope this helps someone in future...thanks for all those who tried to contribute..

Categories