I have successfully hosted a WCF .svc service along with its .NET application on my hosting account @ DiscountASP.net.
The service is now hosted and I can access it from another aspx hosted on the same hosted in the same environment. In that aspx I use Javascript code to access it and its working fine.
Now, what I am trying to achieve here is, access that same service from somewhere else on the web thru .Ajax function which resides in one of my Android applications html pages.
I believe that is possible but I am unable to get that code done.
Below is my CostService.svc code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
namespace SandwichServices
{
[ServiceContract(Namespace = "SandwichServices")]
[AspNetCompatibilityRequirements(RequirementsMode =AspNetCompatibilityRequirementsMode.Allowed)]
public class CostService
{
[OperationContract]
public double CostOfSandwiches(int quantity)
{
return 1.25 * quantity;
}
// Add more operations here and mark them with [OperationContract]
}
}
And below is the code calling it from javascript
var Type;
var Url;
var Data;
var ContentType;
var DataType;
var ProcessData;
function WCFJSON() {
var qq = "3";
Type = "POST";
Url = "rent-o-buy.com/costservice.svc";
Data = '{"quantity": "' + qq + '"}';
ContentType = "application/json; charset=utf-8";
DataType = "json";
ProcessData = true;
CallService();
}
// Function to call WCF Service
function CallService() {
//alert('before ajax call');
$.ajax({
type: Type, //GET or POST or PUT or DELETE verb
url: Url, // Location of the service
data: Data, //Data sent to server
contentType: ContentType, // content type sent to server
dataType: DataType, //Expected data format from server
processdata: ProcessData, //True or False
success: function (msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
//alert('after ajax call');
}
function ServiceFailed(result) {
alert('failed: ' + result.status + '' + result.statusText);
Type = null;
varUrl = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;
}
function ServiceSucceeded(result) {
alert('ServiceSucceeded');
if (DataType == "json") {
resultObject = result.GetUserResult;
alert('resultObject.length=' + resultObject.length);
for (i = 0; i < resultObject.length; i++) {
alert(resultObject[i]);
}
}
}
function ServiceFailed(xhr) {
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
</script>
I need help with the JavaScript because it does not seem to be communicating with the service. Thanks