How to use jQuery to call asp.net asmx web service with parameters to get response
[System.Web.Script.Services.ScriptService]
have to be enabled on the class
inheriting System.Web.Services.WebService
to access its methods via ajax
WebService
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebServiceFile : System.Web.Services.WebService
{
[WebMethod]
//[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string GetProductStock(string productId, string TerminalId, string CCPId)
{
ProductsDomain objProduct = new ProductsDomain();
objProduct.Product_Id = Convert.ToInt32(productId);
objProduct.TerminalId = Convert.ToInt32(TerminalId);
objProduct.CCPId = Convert.ToInt32(CCPId);
DataTable dt = objProduct.GetProductDetail();
if (dt.Rows.Count > 0)
{
return dt.Rows[0]["CurrentQty"].ToString();
}
else
{
return "0";
}
}
public class ProductsDomain
{
public int Product_Id { get; set; }
public int TerminalId { get; set; }
public int CCPId { get; set; }
public DataTable GetProductDetail()
{
DataTable dt = new DataTable();
dt.Columns.Add("CurrentQty");
DataRow dr = dt.NewRow();
dr["CurrentQty"] = "Smith";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1["CurrentQty"] = "John";
dt.Rows.Add(dr1);
return dt;
}
}
}
Jquery
function checkStock() {
var txt_PId = 1;
var txt_TerminalId = 2;
var txt_CCPId = 3;
var msg = '{productId:"' + txt_PId + '",TerminalId:"' + txt_TerminalId + '",CCPId:"' + txt_CCPId + '"}';
debugger;
$.ajax({
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
url: "/WebServiceFile.asmx/GetProductStock",
data: msg,
dataType: "json",
success: function (Result) {
alert();
Result = Result.d;
// data = Result
alert(Result)
},
error: function (Result) {
debugger;
alert("Error: " + Result.error.toString());
return false;
}
});
}
Calling .asmx webservice from jQuery: GET is not allowed?
I can think of two options:
1) Use a POST instead of a GET in your AJAX call:
type: "POST",
or 2) If you must use a GET, configure your web service method to allow GETS:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public List<Tutor> gettutors(string data)
{
var tutorManager = new TutorManager();
return tutorManager.GetTutorApplicants();
}
and allow GETS via web.config:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
calling asmx web service method with jquery ajax
I think you're just running into a same origin policy.
Because of the port difference, the browser is restricting the javascript from reaching the webservice. Of course, there are some workarounds.
Calling ASP.NET ASMX Web Service from jQuery
You are not defining which method to call on the service. Try changing your JQuery line url: '/Services/TeamPerson.asmx',
to url: '/Services/TeamPerson.asmx/SetPlayerStatus',
Using Jquery to call an ASMX service in sharepoint 2010
To get this to work I had to add the following to the web.config file.
<system.web>
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices>
Related Topics
String Concatenation VS String Builder. Performance
C# - Fill a Combo Box with a Datatable
What Is the Fastest Way to Create a Checksum for Large Files in C#
How to Run and Interact with an Async Task from a Wpf Gui
Binding Listbox to List<Object> in Winforms
How to Prevent a SQL Injection Escaping Strings
.Net 4.0 and the Dreaded Onuserpreferencechanged Hang
How to Bind Crystal Report to Manually Created Dataset
Get SQL Query from Linq to SQL
Set Default Global JSON Serializer Settings
Why Can't I Assign a List<Derived> to a List<Base>
Gps Socket Communication (Concox)
How to Change the Background Color of All Other Forms from One Form
How to Detect If Type Is Another Generic Type
Change Border Color in Textbox C#
Should You Implement Idisposable.Dispose() So That It Never Throws