我已在我的托管帐户@ DiscountASP.net上成功托管了WCF .svc服务及其.NET应用程序。
该服务现在已托管,我可以从同一环境中托管在同一托管服务器上的另一个aspx访问它。在那个aspx中,我使用Javascript代码来访问它并且工作正常。
现在,我想在这里实现的是,从网络上的其他地方访问相同的服务,通过.Ajax函数访问我的Android应用程序html页面。
我相信这是可能的,但我无法完成代码。
以下是我的CostService.svc代码
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]
}
}
以下是从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>
我需要JavaScript的帮助,因为它似乎没有与服务进行通信。 谢谢