E.Meir
E.Meir

Reputation: 2226

not able to connect to Web Service

i am trying to learn how to work with web services and ajax through jquery and i have a problem which i don't know how to solve

i have an html page contains:

<!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#btn').click(function () {
            $('div').load('AjaxServices.asmx/HelloWorld');

        });
    });
</script>
</head>
<body>
    <h1>ws</h1>
    <button type="button" id="btn">get info</button>
    <p>the site says...</p>

    <div id="one"></div>
    <div id="two"></div>
    <div id="three"></div>
</body>

asmx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

namespace ajaxTutorial
{
[[WebService(Namespace = "http://appdev.com/jQueryAjax")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
 [System.Web.Script.Services.ScriptService]
public class AjaxServices : WebService
{
    private static int count = 0;

    [WebMethod]
    public string HelloWorld()
    {
        count++;
        return "Hello World" +count.ToString();
    }
}
}

-This is the error i get on chrome:

enter image description here

-This is the solution explorer:

enter image description here

and this is the port that is being opened: enter image description here

*I really dont know what happened, can anyone guide me through this?

what choices do i have here to solve it?

Thanks in advanced. (and yes, i know its an old technology, i'll learn using wcf later on)

Upvotes: 2

Views: 221

Answers (2)

Stephen Oberauer
Stephen Oberauer

Reputation: 5395

This line:

$('div').load('AjaxServices.asmx/HelloWorld');

...calls the web service method by using HTTP GET. For security reasons this was disabled by default in .NET 1.1.

This is explained at http://support.microsoft.com/kb/819267

In order to enable it you need to add this in your web.config under <system.web>:

<webServices>
  <protocols>
    <add name="HttpGet"/>
  </protocols>
</webServices>  

Upvotes: 2

pete
pete

Reputation: 25091

UPDATE:

Try using this for your button handler/AJAX load:

$(document).ready(function () {
    $('#btn').click(function () {
        var p = $('<p />');
        //$('div').load('AjaxServices.asmx/HelloWorld');
        $.ajax({
            "url": "AjaxServices.asmx/HelloWorld",
            "error": function(jqXHR, textStatus, errorThrown) {
                $('div').empty().append(p.clone().text(textStatus)).append(p.clone().text(errorThrown));
            },
            "success": function(data, textStatus, jqXHR) {
                $('div').empty().append(p.clone().text(textStatus)).append(p.clone().text(data));
            }
        });
    });
});

This way the full error should at least appear in your browser.

ORIGINAL:

Define HelloWorld as static:

[WebMethod]
public static string HelloWorld()
{
    count++;
    return "Hello World" +count.ToString();
}

Upvotes: 0

Related Questions