0

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

4

2 に答える 2

1

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

有効な json ではありません。

有効な

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

実行するには、パラメータを onwebmethodからobject[]toに変更するだけですDictionary<string,object>

前回のコメントの続きとして、私の投稿をもう 1 つの解決策で更新します。

Aspx ページ

<%@ 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コード

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 
    }
}

}

于 2013-06-27T11:20:11.047 に答える
0

var requestParameter = { 'xx': x, 'yy': y, 'name': nome };に変更

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

も追加

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

[WebMethod]

class add の宣言の前にも

[System.Web.Script.Services.ScriptService]

このタグは、スクリプトから Web メソッドを呼び出せるようにするために必要です

あなたのウェブサービスはこのようにする必要があります

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

と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');
            }
        });
于 2013-06-27T11:41:26.150 に答える