1

I am following various online examples without any success. I am merely trying to create an initial example that passes a value to a web service call.

What am I doing wrong?

I can do this EASILY with HttpHandlers...something this simple shouldn't be THIS hard?

UPDATED:
The reason it was failing is because the "contentType" attribute was missing. This was outlined in the following answer.

Here Is The Error I Keep Getting:

System.InvalidOperationException: Missing parameter: key.\r\n at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)\r\n at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)\r\n at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()\r\n at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

This Is The HTML:

<script type="text/javascript">

    var url = '<%=ResolveUrl("~/Services/ProjectDialog.asmx/TryThis")%>';

    function callWebService() {

        jQuery.ajax({
            cache: false,
            type: 'POST',
            complete: onComplete,
            data: '{ "key": 42 }',
            dataType: 'application/json; charset=utf-8',
            error: onError,
            success: onSuccess,
            url: url
        });
    }

    function onComplete(status, xmlHttpRequest) {
        var stop = "";
    }
    function onError(xmlHttpRequest, status, error) {
        var stop = "";
    }
    function onSuccess(data, status, xmlHttpRequest) {
        var stop = "";
    }

    jQuery(document).ready(function() {
    });

</script>

<input type="button" value="Run Web Service" onclick="callWebService();" />

This Is The Web Service:

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

namespace My.Services
{
    /// <summary>
    /// Summary description for ProjectDialog
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class ProjectDialog : System.Web.Services.WebService
    {
         [WebMethod]
         [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
         public string TryThis(Int32 key)
         {
             return key.ToString();
         }
    }
}
Community
  • 1
  • 1
Prisoner ZERO
  • 13,848
  • 21
  • 92
  • 137

4 Answers4

5

Try replacing the following line:

data: '{ "key": 42 }',

With:

data: JSON.stringify(data),

Where data is a variable declared earlier as:

var data = { key : 42 };

This is taken from the following answer: Calling .Net webservice with Jquery is causing woe when trying to post data

UPDATED

The above suggestion was not the problem, as noted by the author, the problem turned out to be that the contentType was not set correctly. However, the above link did point to this.

Community
  • 1
  • 1
Jim Liddell
  • 514
  • 3
  • 13
  • Your original REASON was actually incorrect...but the referring article DID answer correctly. Which is why you should get the credit. – Prisoner ZERO Apr 05 '11 at 13:35
1

I think your problem is in how you're setting up your data parameter, I don't think you need the quotes:

data: ({ key: 42 }),
Paddy
  • 33,309
  • 15
  • 79
  • 114
  • 1
    You definitely don't need the single-quotes (apostrophes) for the JSON-representation of your data. – El Guapo Apr 05 '11 at 12:57
0

I think your quotes may need changing;

 data: '{ "key": 42 }',

should be

data: "{ 'key': 42 }",
bleeeah
  • 3,534
  • 19
  • 25
0

Can you please try to GET and pass the "key" as a parameter in the URL?

ysrb
  • 6,693
  • 2
  • 29
  • 30
  • Thanks for the reply...but doing this generates a rather long responseText error starting with: Request format is unrecognized for URL unexpectedly ending in '/TryThis'.: – Prisoner ZERO Apr 05 '11 at 13:02