Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Asp上使用Json时出错_C#_Javascript_Asp.net_Json_Post - Fatal编程技术网

C# 在Asp上使用Json时出错

C# 在Asp上使用Json时出错,c#,javascript,asp.net,json,post,C#,Javascript,Asp.net,Json,Post,我在aspnet上使用JavaScript和C。我想将3个值从Asp页面传递给代码隐藏,为此我使用Json方法。 我是这样做的: //initialize x, y and nome var requestParameter = { 'xx': x, 'yy': y, 'name': nome }; $.ajax({ type: 'POST', url: 'Canvas.aspx/GetData

我在aspnet上使用JavaScript和C。我想将3个值从Asp页面传递给代码隐藏,为此我使用Json方法。 我是这样做的:

   //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"); }
            });
      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>
然后在C#上,我会:

      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>
出于某种原因,我不断收到“error”(我在ajax post方法中定义的)警报。我想知道为什么,以及如何避免这种情况。 提前感谢

 { 'xx': x, 'yy': y, 'name': nome }
      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>
不是有效的json

      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>
有效的是

 var requestParameter = { "xx": 1, "yy": 11, "name": "test" }
      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>
要运行,只需将
webmethod
上的参数从
object[]
更改为
Dictionary

      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>
作为你最后一条评论的继续,我用另一个解决方案更新我的帖子

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

      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>

      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>
函数testmethod()
{
var requestParameter={“xx”:1,“yy”:11,“name”:“adadsaasd111”};
PageMethods.测试(参数);
}
功能测试()
{
testmethod();
}

      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代码

      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>
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 
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
命名空间WebApplication1
{
公共部分类WebForm1:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
[System.Web.Services.WebMethod]
公共静态无效测试(字典cal)
{
//待办事项
}
}

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

      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>
var requestParameter = { "xx": "'+x+'", "yy": "'+y+'", "name": "'+nome+'" };
还加

      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>
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]

      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>
同样在类add的声明之前

      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>
[System.Web.Script.Services.ScriptService]
允许从脚本调用web方法需要此标记

      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>
您的Web服务应该是这样的

      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>
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetData(String xx, String yy, String name)
{
    return xx+yy+name;
}
和jquery

      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>
 $.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');
            }
        });

您的web方法可能不会返回有效的JSON。执行
console.log(arguments)
而不是
alert(“error”)
,您将在控制台中看到确切的消息。最喜欢的是“无效字符”。您需要检查响应的状态代码-使用FIDER、Firebug或Chrome DevToolbar。若状态为404,则无法访问该方法(可能是因为该方法标记为静态)。否则,将代码更新为以下内容:error:function(jqXHR,textStatus,errorhown){alert(textStatus);alert(errorhown);}这是请求参数,jquery ajax方法允许传递对象。@Tommi你是说如果你用直接方法传递对象,它就会播放???我说jquery会正确地序列化这个对象,并将它作为有效的json写入POST请求体。我没有说任何关于服务器端的事。嗯,不,不是,对不起。它将其序列化为
xx=1&yy=2&name=nome
Ok,我将更改我的帖子。你必须做一些改变。嗨,谢谢你到目前为止的帮助。你说的我都试过了,但还是犯了同样的错误。可能会错过什么?我不明白,还是什么都没有。。。我是不是错过了图书馆或其他什么?