Page not found error when calling webservice method with jquery - asp.net-mvc-3

I have created a WebService directory in my MVC web application's root folder and created a webservice named MyService.asmx. In this file, I created an method UpdateReadCount, which I call from javascript with below code.
function updateReadCount(contentID) {
$.ajax({
type: "POST",
url: "/WebServices/MyService.asmx/UpdateReadCount",
data: "{'contentID': '" + contentID + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (json) {
//$("#" + messageView).html(json.d);
}
});
}
I am getting Page Not Found error. This is happening at hosted page, while it works perfectly on localhost.

Why use a .asmx with an MVC application? You can just do a clean controller action for this.

Related

Calling ASP.NET MVC 4 Controller from Javascript

I'm calling an ASP.NET MVC 4 control method from Javascript (in a cshtml file) using $.ajax() as shown below
$.ajax({
url: '#Url.Action("MyAction", "MyController")',
type: 'GET',
data: { 'id': "123"},
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
}
});
The controller action method is
public JsonResult MyAction(string id)
{
// Do stuff
return new JsonResult();
}
which is getting called ok but is causing a GET 500 (Internal Server Error).
I don't really care about the returned data I just want to call the controller method to update a model.
Can anyone let me know why I'm getting the 500 or an alternative way of doing this that would be great.
For security reasons you cannot use the GET method in ajax requests (See JSON Hijacking).
You just have to do it like this:
return Json(data, JsonRequestBehavior.AllowGet)
or better off, change the method to post
type: 'POST',

Dotnetnuke WebService with jquery Post

I am developing a dotnetnuke module and have lots of issues connecting to a webservice through javascript to retrieve data.
I have the folloiwng javascript:
function Test() {
$.ajax({
type: "POST",
url: 'http://localhost/DNN11/service1.asmx/TestMethod',
data: "{'data':'" + "abc" + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert("success");
},
error: function(e) {
alert("error");
}
});
}
Then the following WebMethod in my webservice
[WebMethod]
public string TestMethod(String data)
{
return "Hello World";
}
However when the post is not being called successfully, I am not able to get through my webservice. It keeps giving me a 500 Internal Server error page.
What could be wrong please? Is there any other way I need to go about this?
Thanks
[Edit]
This is the entire code
<script type="text/javascript">
function Test() {
$.ajax({
type: "POST",
url: '<%=ResolveUrl("~/DesktopModules/ModuleTest/WebService1.asmx/TestMethod")%>',
data: '{ data: "test" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert("Success: " + msg);
},
error: function(msg) {
alert("Failed: " + msg.status + ": " + msg.statusText);
}
});
}
</script>
<div>
<input type="button" value="Load" onclick="Test(); return false;" />
</div>
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod()]
public string TestMethod(string data)
{
return "Hello World";
}
}
I have tried putting the Webservice in the DotNetNuke folder in a new folder and both in the local project.. I also put the ScriptMethod() Tag in the WebService which allows scripts to call it.. but still nothing
Looks like your problem is the cross domain calls. In case if your page hosted on the same server with the web service, your code should be fine. If you need to interact with the service from another domains you have to use jsonp requests. Look at this good step-by-step guide with the code example here.
UPDATE
In case if everything on the same app try to use relevant URL. Instead of url: '<%=ResolveUrl("~/DesktopModules/ModuleTest/WebService1.asmx/TestMethod")%>' try to url: '/DesktopModules/ModuleTest/WebService1.asmx/TestMethod'.
In order to post to a DNN webservice from the page you're going to have to pass in the Context of the module. I have some sample code that works against DNN6 at
http://slidepresentation.codeplex.com/SourceControl/changeset/view/69709#1181962
Mainly the
servicesFramework: $.ServicesFramework(<%=ModuleContext.ModuleId %>)
line
I haven't tested that against DNN7 though with the latest service framework WebAPI changes.

In MVC calling webservice with ajax call not working give error code 404?

In my .net framework with MVC calling webmethod like. webservice1.asmx/helloWorld
with Ajax give error 404 not found..In my another server same code working. Is there
anything missing to call ?? and physical path give me same webserive and webmthod in my .net project.. please help me ..
EDIT
code to call the web service
$.ajax({
type: "POST",
url: "/WebServices/WebService1.asmx/HelloWorld",
data:"{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(msg) {
var data = msg.d;
},
error: function(msg) {
alert(msg);
}
});
I suspect that you have hardcoded the url to the web service in your javascript call instead of using an url helper to generate it. So try like this:
<script type="text/javascript">
$.ajax({
type: "POST",
url: "#Url.Content("~/WebServices/WebService1.asmx/HelloWorld")",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(msg) {
var data = msg.d;
},
error: function(msg) {
alert(msg);
}
});
</script>
Notice how the url to the web service is no longer /WebServices/... but it is generated with an url helper. So if for example you deploy your application in a virtual directory in IIS the helper will take into account this virtual directory.

Jquery Webservice in ASP.NET

I am working with ASP.NET 4.0 framework. In one web page, I have to call web service using Jquery as
var serviceurl = 'http://www.websitename.com/webservicename';
$.ajax({
type: "POST",
url: serviceurl + 'WebServiceName',
data: "{'Parameters': '" + parameter+ "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
ShowAfterSuccess(msg);
},
error: AjaxFailed
});
It works fine, if i mention url as "http://www.websiteName.com" but when i put URL as "websitename.com" it doent call webservice.
but it works well only in Google Chrome with "websiteName.com" I dont knw what is the issue with that....whether there is problem in my webservice calling or in URL..
You must ensure that you are not violating the same origin policy restriction. The best way to ensure this is to use relative urls:
var serviceurl = '/webservicename';
You must ensure that the domain hosting this javascript matches exactly the domain that you are sending your AJAX call to (including the protocol).

jQuery Ajax Response: getting response as null on success

I wanted to invoke a .net webservice which is hosted locally on my machine from jQuery .ajax().
the script is included in the static .html file.
the success function is triggered but the response data is NULL.
$.ajax({
type: "GET",
data: '{continent: "' + $('#txtContinent').val() + '"}',
url: "http://localhost:60931/Service1.asmx/GetCountries",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
alert(response);
},
failure: function(msg) {
$('#result').empty().append(msg);
}
});
When i alert the response its giving me NULL,
can anyone tell me where im goin wrong, Thanks in advance.
Its because you might be calling the ajax from a static page which is not in http://localhost:60931/. You can put the static html in the localhost and try to run it via the same url. Or you can debug your ajax call by adding
error : function(xhr,status,error){
alert(status);
},

Resources