I have two projects in my Visual Studio solution: MyApp.WebService and MyApp.WebUI.
I have a view page in my WebUI project at /Views/Home/Index.aspx, making this ajax call:
$.ajax({
type: "GET",
url: "MyService.svc/HelloWorld",
data: null,
processData: true,
contentType: "application/json",
dataType: "json",
cache: false,
success: function (data) {
alert(data.d);
}
});
I have a .svc file in the root of my WebService project, named MyService.svc, which contains this function:
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public string HelloWorld()
{
return "Hello, world!";
}
I'm getting an error saying it can't find the location of the url I'm 开发者_如何学JAVAcalling. I suspect it has something to do with the .svc file being in another projects. Is there something I need to do to call it?
If you use two different projects, then either the projects are hosted in different ports (the default for VS) or in different virtual directories. Either way the request from one project to the other like you're doing won't work. If you have two projects in different ports (e.g., MyApp.WebService at 6789 and MyApp.WebUI at 7890), then the call from /Views/Home/Index.aspx (which is actually http://machine-name:7890/Views/Home/Index.aspx) to MyService.svc/HelloWorld will be made to http://machine-name:7890/MyService.svc/HelloWorld, while it should have been made to http://machine-name:6789/MyService.svc.HelloWorld. Running Fiddler while opening the page will show you the address which the call is being made. Also, if you right-click on MyService.svc on VS and select "View in Browser" it will show you the exact address where the call should be made.
精彩评论