I want to call a webservice from javascript.
This is my code:
var method="GetStock";
var url = "http://www.mywebsite.ro/ServiceGetStock.asmx";
$.ajax({
type: "POST",
url: url + "/GetStock",
data: "{variant_id='1'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall,
error: OnErrorCall
});
function OnSuccessCall(response) {
alert(response.d);
}
function OnErrorCall(response) {
alert(response.status + " " + response.statusText);
}
My ServiceGetStock.asmx code:
[WebMethod]
public string GetStock(int variant_id)
{
try
{
ProductVariant variant = ProductVariantManager.GetProductVariantByID(variant_id);
return variant.Stock.ToString();
}
catch (Exception ex)
{
return ex.Message;
}
}
I got the error message:
POST http://www.mywebsite.ro/ServiceGetStock.asmx/GetStock 500 (Internal Server Error)
[UPDATE]
I forgot to mention that I added in webconfig of project(with webservice) because I got the error:
XMLHttpRequest cannot load http://www.mywebsite.ro/ServiceGetStock.asmx/HelloWorld. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http:// localhost:11300' is therefore not allowed access.
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
Adding Reference of the Web Service in Visual Studio 1. Right click the project in Solution Explorer and choose Add Service Reference option from the context menu. 2. Now in the Add Service Reference Dialog you need to click on the Advanced button.
ASMX provides the ability to build web services that send messages using the Simple Object Access Protocol (SOAP). SOAP is a platform-independent and language-independent protocol for building and accessing web services.
Ok guys. I found the problem. When an ASMX file is created, you must read all comments lines. To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
//[System.Web.Script.Services.ScriptService]
So the GetStock function is:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetStock(string variant_id)
{
SendEmail.SendErrorMail("in"+ variant_id);
try
{
ProductVariant variant = ProductVariantManager.GetProductVariantByID(Convert.ToInt32(variant_id));
return variant.Stock.ToString();
}
catch (Exception ex)
{
return ex.Message;
}
}
and the Ajax code is:
var url = "http://www.mywebsite.ro/ServiceGetStock.asmx";
$.ajax({
type: "POST",
url: url + "/GetStock",
data: "{variant_id:'1'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall,
error: OnErrorCall
});
function OnSuccessCall(response) {
alert(response.d);
}
function OnErrorCall(response) {
alert(response.status + " " + response.statusText);
}
Problem solved! Thanks all for tips.......
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With