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);
}
});
}
});
Related
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.
I have an WebService in C# and I want to access this webservice from another application.
Ex. Have one webservice running in localhost and i also have a website running in localhost, and this two projects are in diferents places. The question is: How do I invoke this webservice from my website with ajax, both in localhost.
The Code that i have is this:
WebService
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public String HelloWorld()
{
return "Hello World";
}
}
and Client
$.ajax({
type: "POST",
url: "localhost:52137/Service1.asmx?op=HelloWorld",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: '',
success: function (data, status) {
alert(data.d);
},
error: function(data, status){
alert(status);
}
});
Try changing url to this:
url: "http: //localhost:52137/Service1.asmx/HelloWorld"
And BTW.. if the website is running on a different port than the service.. you still have xdomain issues.
CORS ASMX
As these two projects are in different places, it would be CORS request.
You need to enabled cross domain request in your service application as suggested in this article.
You can use $.getJSON which allow cross domain request.
As you're using C# you can create HTTP Handler as shown in this article.
Let me start of by saying I am very new to ASP.NET and C#.
I have a simple web form containing data which I want to send to a code-behind page.
The idea is to capture the data and send it as a JSON object to a code-behind method.
Note that this is done via JavaScript/AJAX (see code below).
The code-behind method will then do a trivial HTTP "PUT" request to update the data.
The .apsx page is located in the Secure folder (uses a Secure Master).
Don't know if that will affect the method calling?
Following is the code I have thus far.
JavaScript/AJAX:
var saveOptions =
{
url: "Profile.aspx/UpdateVendor",
type: "PUT",
dataType: 'json',
data: JSON.stringify({ vendor: ko.mapping.toJS(vendor) }),
contentType: "application/json",
success: function (response)
{
}
}
Code-behind:
namespace PartyAtVendors.Secure
{
[WebService]
public partial class Profile : System.Web.UI.Page
{
[WebMethod]
public static bool UpdateVendor(PartyAtApi.Models.Vendors vendor)
{
return true;
}
}
}
Update:
Problem is as follows. The codebehind method isn't called. When I run and test the code and use Chrome's "inspect element" I receive the error:
PUT http://localhost:50671/Secure/Profile.aspx/UpdateVendor 404 (Not Found)
Such static methods are called Page Methods in asp.net.
You call them form javascript like this on the same page as follows:
string data = "{mydata:myvalue}";
PageMethods.UpdateVendor(data);
You need to have a ScriptManager on the page and it should have PageMethods enabled
<asp:ScriptManager runat="server" EnablePageMethods="true">
</asp:ScriptManager>
The page method must be defined on the page. It cannot be defined in a control, master page, or base page.
PageMethods only support HTTP POST(Even when you call them without the javascript proxy of PageMethods). You can read the details of this security limitation in this blog post by Scott Guthrie. Using the PUT verb is causing the 404 error.
ASP.NET AJAX 1.0 by default only allows the HTTP POST verb to be used
when invoking web methods using JSON
Your webmethod & javascript method should be on same page.
Hi managed to sort out what was wrong.
I simply changed the HTTP method to "POST" as follows:
var saveOptions =
{
url: "Profile.aspx/UpdateVendor",
type: "POST",
dataType: 'json',
data: JSON.stringify({ vendor: ko.mapping.toJS(vendor) }),
contentType: "application/json",
success: function (response)
{
}
}
$.ajax(saveOptions);
This seemed to fix the problem and now I am able to send the JSON data to the codebehind method using AJAX.
I have a .NET webservice which I need to hit asynchronously from the jQuery and update the grid based on the result got from service. My problem here is that, the service hit is async only for the first time and the subsequent requests are sync (even after specifying async=true in the ajax call) as shown below
jQuery.ajax({
type: "POST",
url: url,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function(msg) {
var result = msg.d;
return callback(result);
},
error: $.callDotNetSM.onError
});
My .NET service method is something like below.
[WebMethod(EnableSession = true)]
public static string GetData()
{
}
So, please help me in calling the service asynchronously all the times. Any help is highly appreciated.
The page your WebMethod is being called on needs to have the following in the Page Directive:
<%# Page Async="true" EnableSessionState="False" %>
The reason for this is that ASP.NET locks the Session every time it is accessed so that it cannot run asynchronously.
Even if you do not use the Session in your WebMethod the page still thinks it uses it by default unless you tell it otherwise.
Edit:
Your problem could also be that you have EnableSession="true". You may just need to set it to false.
I'm using ajax in my website to call some information from a UserControl called NewsFeed.ascx which is found in the folder 'controls', my way is to make a web service page (in a folder called WebMethods) which contain a function in my case called GetRSSReader which returns a string format:
[WebMethod]
public string GetRSSReader()
{
Page page = new Page();
UserControl ctl =
(UserControl)page.LoadControl("~/Controls/NewsFeed.ascx");
page.Controls.Add(ctl);
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);
return writer.ToString();
}
then I call this page using Jquery (which I found it too heavy) to get the returned data as a JSON like this :
<div id="Content"></div>
<script type="text/javascript" defer="defer" src="../JAVA/Default.js"></script>
>
$(document).ready(Update);
function Requests()
{
$.ajax({
type: "POST",
url: "../WebMethods/Feed.asmx/GetRSSReader",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
$('#Content').html(msg.d);
}
});
}
the Jquery.js and this page (default.js) founded in the folder Java
my question : can I not to use webService and instead using WCF !!! and how !?
What you refer to as "web services" is the old "ASMX Web Service" feature of .NET (sometimes known as ASP.NET Web Services).
WCF is the replacement for ASMX web services.
See https://stackoverflow.com/tags/wcf/info for some getting-started information.
If you're just returning JSON then I highly recommend you simply use an HttpHandler instead of some combination of WCF, SOAP, UserControls, and whatever else you're throwing in there. Here's a quick tutorial on the subject. You'd save yourself the overhead of page life-cycle stuff that you don't need. And returning the JSON is as simple as serializing your return values with the JavaScriptSerializer.