Question

I have my javascript/jquery front end like so:

var jsonData = JSON.stringify({
            'myType': 'typeOfFile',
            tmyData:
            {
                'SelectedYears': myArray["SelectedYears"],
                'columns': myArray["Columns"],
                'rows': myArray["Rows"],
                'filters': myArray["Filters"]
            }
        });

        alert(jsonData);

        $.ajax({
            type: "POST",
            url: "mywebservice.asmx/Create",
            data: jsonData,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (xhr, status, error) {
                //alert the error if needed
                //$("#result").html("Sorry there is an error: "   xhr.responseText);
                alert('Error: ' + xhr.responseText);
            },
            success: function (responseData) {
                alert(responseData);
                // show the response data from webservice. Note: the d represent the object property data passed by webservice. It can an object of properties or just single property
                //$("#result").html("The id is " + responseData.d.ID + " And Name is " + responseData.d.Name + " And Email is " + responseData.d.Email);
            }
        });

My webservice.asmx file:

[WebService(Namespace = "http://tempuri.org")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class mywebservice : System.Web.Services.WebService
{

    [WebMethod]
    public string Create(string myType, string myData)
    {
        //JToken token = JObject.Parse(myData.ToString());

        //JObject obj = (JObject)token.SelectToken("SelectedYears");

        return "Hello World";
    }
}

This above crashes because the second parameter I want as a string but it isn't accepted. It works if I change it to object. How do I get it to a string so I can use JSON.Net to parse it?

No correct solution

OTHER TIPS

I have to say that I haven't tried it, but my guess is that you have to change the type of myData to a matching object as you have your js object not a string.

for example have:

class MyData
{
   // Whatever are your properties ex: SelectedYears, columns etc..
}

change the signature to

public string Create(string myType, MyData myData)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top