0

I am trying to simply pass some values from Javascript into my codebehind for processing. I just want to pass a Number and Message to the WebMethod but get different errors based on whatever thing I change.

JS:

function SendMessage() {
var number = document.getElementById("number").value;
var message = document.getElementById("message").value;

var msg = {
    "Number": number,
    "Message": message
};

$.ajax({
    type: "POST",
    url: "Default.aspx/SendMessage",
    data: JSON.stringify(msg),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert("Message sent");
    },
    error: function (msg) {
        alert("Message call failed");
    }
});
}

In my codebehind I have a WebMethod defined and also a Message class to hold my message

[WebMethod]
public static void SendMessage(string message)
{
    //Create a  TMessage and deserialize to it

}

Message:

public class TMessage
{
    public string Number { get; set; }
    public string Message { get; set; }
}

My understanding is that I am receiving JSON and should deserialize it to a Message type. However I have a breakpoint in the SendMessage method and it never gets hit, the error being returned is: Message=Invalid web service call, missing value for parameter: 'message'.

From some playing around earlier, I was able to get the breakpoint hit by changing the parameter from string to object (and some changes to the data: value in the Ajax call), but then it seemed I was receiving a Dictionary and I couldn't cast it to TMessage.

Thanks for any suggestions.

cdsln
  • 830
  • 1
  • 11
  • 33

1 Answers1

1

If you clearly look at webmethod , it has only one param i.e. message .

You need to pass this to ajax call. - var params = "{'message':'" + message + "'}";

Helpful link - Send JSON to webmethod?

function SendMessage() {

var number = document.getElementById("phonenumber").value;
var message = document.getElementById("message").value;

var params = "{'message':'" + message + "', 'number':'" + number + "'}";
$.ajax({
    type: "POST",
    url: "Default.aspx/SendMessage",
    data: params,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert("Message sent");
    },
    error: function (msg) {
        alert("Message call failed");
    }
});
}
Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • Tried this and the breakpoint is hit, but the value for the `message` parameter is: `"[object Object]"` – cdsln Oct 18 '14 at 16:43
  • Where? is it in `success` handler for `ajax`? If yes, then just use `msg.d` . I mean , does ajax call goes well with HTTP status 200? – Arindam Nayak Oct 18 '14 at 16:45
  • The breakpoint is hit in the codebehind- in `SendMessage(string message)`. But the value of `message` is `"[object Object]"`. I expected it to be something along the lines of a JSON string representation of the values I passed in the Ajax call...? – cdsln Oct 18 '14 at 16:48
  • does - `document.getElementById("message").value` has actual string value? – Arindam Nayak Oct 18 '14 at 16:49
  • 1
    Actually I had the `var params = "{'message':'" + message + "'}";` set to `var params = "{'message':'" + msg + "'}";` It works now but only sends the `message` and not the number.. It's a start anyway :) – cdsln Oct 18 '14 at 16:59
  • Glad to know that worked. To use number, first you have to make another input parameter in `SendMessage` and in same manner pass number to webmethod. – Arindam Nayak Oct 18 '14 at 17:01