Dotnetnuke WebService with jquery Post - ajax

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.

Related

$.ajax POST prompting authentication dialog

I am trying to call a WebMethod using AJAX POST but the browser keeps opening an 'authentication dialog'. I've not encountered this problem before using similar code detailed below.
In my search.aspx file I have the following:
HTML
<a href="javascript: ExpandChild('div4');" runat="server"> <img alt="Students" id="imgdiv4" src="images/arrow-right-b.png" />
</a>
JS
function ExpandChild(input)
{
//somethings are done here
LoadStudentData();
}
function LoadStudentData()
{
$.ajax({
type: "POST",
url: 'webmethods.aspx/TestCall',
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: 'json',
success: function (data) {
alert("ajax called");
},
error: function (result) {
alert("An unknown error occurred, please try again: " + result.responseText);
}
});
}
In my webmethods.aspx file I have the following:
[WebMethod()]
public static string TestCall()
{
return "it worked";
}
I know the code gets the to the LoadStudentData method but I have no idea why it is then opening an authentication dialog. Both the search.aspx page and the webmethods.aspx page are in the root of my project
I am using Visual Studio 2015 and this problem occurs in Chrome and Firefox when run on my local machine. When debugging the code it never gets to the TestCall method.
Any help much apprecciated.
Following this article
http://www.aspforums.net/Threads/105222/Error-SystemInvalidOperationException-Authentication-failed/
In the App_start/RouteConfig.cs I changed
settings.AutoRedirectMode = RedirectMode.Permanent;
To
settings.AutoRedirectMode = RedirectMode.Off;
Then the ajax post worked fine.

Why i can not call action method with ajax?

I'm new in asp.net mvc want to call action method in the view page,for that purpose write this ajax code:
$('#SaveBTN').click(function (e) {
$.ajax({
url: "/Home/SaveRecord",
type: "POST",
data:'', //JSON.stringify({ 'Options': someData }),
dataType: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function (data) {
alert("Done");
},
error: function () {
alert("An error has occured!!!");
}
});
});
this is my button:
ثبت نام
and this is my action:
[HttpPost]
public ActionResult SaveRecord()
{
return Json("chamara", JsonRequestBehavior.AllowGet);
}
but when run the web application and fire button,get error segment alert in ajax ,How can i solve that problem?thanks all.
With the question and code presented in the way it is there is no way to help. I recommend starting from ground zero and stepping through this tutorial.
Basically that^ link will get you through the basics of using web api in MVC to do AJAX calls and populate the page. There are some other methods however this is the best place for trying to learn what you're studying.

How to consume a rest service hosted on remote server in asp.net mvc3 using jquery ajax ?

I have a rest service. I hosted this service to IIS in my local system and consumed in an mvc3 application. It run without any error. But in the same application, I am not able to consume the rest service hosted on remote server. I have written following jquery to access it :
<script type="text/javascript">
$(document).ready(function () {
$("#btn").click(function () {
$.ajax({
url: "http://portno/Student.svc/AutoComplete?Branch=Civil&RecordLimit=4&s_name=Linda",
type: "GET",
contentType: "application/json; charset=utf-8",
data: {},
dataType: "xml",
success: function (data) {
alert(data.documentElement.childNodes[3].textContent);
},
error: function (error) {
alert("Error: " + error.text);
}
});
});
});
is there any mistake here? Please help me. Thank you.

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 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