-2

I have an AJAX code where I am trying to send some data to a method within C# code-behind

I've tried to send them either using text data type and json data type, but neither of them are working with me.

when I use json type, it returns me the following error:

Internal Server Error

and when using text me thod, there is no error appear and the code comes through the success function, but the actually the data is never sent to the method of the code-behind class

this is the ajax code using json type:

function searchClicked(sCriterion) {
        $.ajax({
            type: "POST",
            url: "TokenRegistration.aspx/GetSearchCritetrion",
            data: "{creiterion : " + sCriterion + " }",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
            },
            success: function (result) {
                alert("We returned: " + result);
            }

And this is the ajax code using the text format:

function searchClicked(sCriterion) {
        $.ajax({
            type: "POST",
            url: "TokenRegistration.aspx/GetSearchCritetrion",
            data: sCriterion,
            contentType: "application/text; charset=utf-8",
            dataType: "text",
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
            },
            success: function (result) {
                alert("We returned: " + result);
            }

Also here is the my code-behind method that the data should be sent to:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetSearchCritetrion(object selectedItem)
{
    var json = new JavaScriptSerializer();
    var data = json.Deserialize<Dictionary<string, Dictionary<string, string>>[]>(selectedItem.ToString());
}

I've used the exact same ajax code in another project and it works perfectly, but here i'm not getting where the error is, so any suggestions please?

2
  • try make the url url: "/TokenRegistration.aspx/GetSearchCritetrion", Commented Oct 9, 2016 at 7:28
  • it's not working dear, @Mostafiz Commented Oct 9, 2016 at 7:31

3 Answers 3

0

see if this works instead of sending object try to pass as string

data: JSON.stringify({selectedItem : sCriterion }),

code behind

public void GetSearchCritetrion(string selectedItem)
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a really simple example that posts JSON to a controller method, which converts this into an object and then returns a response.

HTML:

<button id="go" class="btn">Go</button>

JS:

    var obj = { foo: 123, bar: "abc " };
    $('#go').click(function () {
        $.ajax({
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            type: 'POST',
            url: '/Test/TestJson',
            data: JSON.stringify(obj),
            success: function (response) {
                alert(response.result)
            },
            failure: function (response) {
                alert('fail');
            }
        });

    });

Controller:

public class TestController : Controller
{
    [OutputCache(Location = OutputCacheLocation.None)]
    public JsonResult TestJson(TestClass tc)
    {
        return Json(new { result = "foo=" + tc.foo + " bar=" + tc.bar }, JsonRequestBehavior.AllowGet);
    }

    public class TestClass
    {
        public int foo { get; set; }
        public string bar { get; set; }
    }
}

Comments

0

I think this help you.

class Item 
{
    public int id { get; set; }
}

[WebMethod]
public static void GetSearchCritetrion(Item selectedItem)
{
    //ToDo: selectedItem.id
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.