简体   繁体   中英

Error while using Json on Asp

I'm using JavaScript and C# on aspnet. And i want to pass 3 values from the Asp Page to the code behind, and to do so i am using Json method. Here it is how i do:

   //initialize x, y and nome
   var requestParameter = { 'xx': x, 'yy': y, 'name': nome };

            $.ajax({
                type: 'POST',
                url: 'Canvas.aspx/GetData',
                data: requestParameter,
                //contentType: "plain/text",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (data) {
                    alert(data.x);

                },
                error: function () { alert("error"); }
            });

And then on the C# i do:

[WebMethod]
public static string GetData(Object[] output)
{
    return output.ToString();
}

For some reason i keep on getting the alert saying "error" (the one that i defined on the ajax post method). I would like to know why, and how can i avoid that. Thanks in advance

The

 { 'xx': x, 'yy': y, 'name': nome }

Is not valid json.

A valid is

 var requestParameter = { "xx": 1, "yy": 11, "name": "test" }

In order to run just change the parameter on webmethod and from object[] to Dictionary<string,object>

As continue of your last comment i update my post with one more solution.

Aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

      function testmethod() 
          {
            var requestParameter = { "xx": 1, "yy": 11, "name": "adadsaasd111" };
            PageMethods.test(requestParameter);
           }

        function test() 
        {
            testmethod();
        }
    </script>

    <input id="Button1" type="button" onclick="return test();" value="button" />
</form>

cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [System.Web.Services.WebMethod]
    public static void test(Dictionary<string,object> cal)
    {
       // todo 
    }
}

}

change var requestParameter = { 'xx': x, 'yy': y, 'name': nome }; to

var requestParameter = { "xx": "'+x+'", "yy": "'+y+'", "name": "'+nome+'" };

also add

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

after [WebMethod]

also before the declaration of class add

[System.Web.Script.Services.ScriptService]

this tag is required to allow web methods to be called from scripts

Your webservice should be like this

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetData(String xx, String yy, String name)
{
    return xx+yy+name;
}

and jquery

 $.ajax({
url: '/Canvas.aspx/GetData',// the path should be correct
            data: '{ "xx": "'+x+'", "yy": "'+y+'", "name": "'+nome+'" }',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            type: 'POST',
            success: function (msg) {
                alert(msg.d);

            },
            error: function (msg) {

                //alert(msg.d);
                alert('error');
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM