Calling Asmx from Jquery

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



Leave a reply



Submit