Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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#方法中接收数据,该方法通过$.ajax()发送_C#_Ajax - Fatal编程技术网

未在C#方法中接收数据,该方法通过$.ajax()发送

未在C#方法中接收数据,该方法通过$.ajax()发送,c#,ajax,C#,Ajax,我正在使用$.ajax()从脚本发送一个复杂字符串(它是样式属性、ID和标签文本的组合)。我就类似的问题问了几个问题。但也许我不明白我哪里弄错了 这是我正在使用的脚本: $(".btnSaveStyle").click(function (e) { var comp1Style = ""; var comp2Style = ""; $(".box").children(".comp1").each(function () {

我正在使用
$.ajax()
从脚本发送一个复杂字符串(它是
样式
属性、
ID
标签文本的组合)。我就类似的问题问了几个问题。但也许我不明白我哪里弄错了

这是我正在使用的脚本:

    $(".btnSaveStyle").click(function (e) {
        var comp1Style = "";
        var comp2Style = "";

        $(".box").children(".comp1").each(function () {
            var style = $(this).attr('style');
            var title = $(this).text();
            var componentClass = $(this).attr('class');
            comp1Style = comp1Style + style + "#" + componentClass + "#" + title + "$";
        });
        alert(comp1Style); //I get the style here

        $.ajax({
            type: "POST",
            async: true,
            url: 'AjaxRecieveStyle.aspx/GetStyle',
            data: comp1Style
        });
在C#中,我通过以下方式访问它:

 [WebMethod]
    protected void GetStyle(string style)
    {
        var recievedStyle = style;

        Customer customer = (Customer)Session["existing_user"];

        if (customer != null)
        {
            EventComponent eventComponent = new EventComponent();
            string txtComp1 = recievedStyle;
            string[] separateComponents = txtComp1.Split('$');
            string[] individualComponent = new string[5];

            foreach (string position in separateComponents)
            {
                individualComponent = position.Split('#');

                if (individualComponent[0].Equals(""))
                {
                    //do nothing
                }
                else
                {
                    eventComponent.EventID = 1;
                    eventComponent.Image = "";
                    eventComponent.Style = individualComponent[0].ToString();
                    eventComponent.ComponentType = individualComponent[1].ToString();
                    eventComponent.Title = individualComponent[2].ToString();

                    int id = new EventComponentLogic().Insert(eventComponent);
                }
            }
        }
    }

现在

1):我应该使用JSON对象来传递数据吗


2):请告诉我我做错了什么?

做了一些挖掘,找到了以下链接:

该网站的源代码显示,您可能缺少ajax调用中的一些关键功能:

function ShowCurrentTime() {
    $.ajax({
        type: "POST",
        url: "Default.aspx/GetCurrentTime",
        data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response.d);
        }
    });
}
虽然成功和失败属性绝对是可选的,但我相信设置您的内容类型和数据类型确实会对您有所帮助。

1)是的,最好使用JSON发送数据-我的意思是,当一年后有人查看该代码时,会更容易理解发生了什么。而且基于JSON扩展协议也容易得多

2) 我建议您在GetStyle(string style)方法的最开始添加日志记录。然后,请尝试在浏览器中显式键入URL(或者更好地使用PostMan-请参阅下面的链接,PostMan将帮助您测试POST请求,因为我看到您在那里有POST请求),并确保web服务器代码正常工作

只有当它工作时,请尝试您的前端AJAX请求

我认为您在WebAPI中没有正确处理POST请求。它将只处理GET请求。有关详细信息,请查看此SO问题:


3) 链接到邮递员:

我将脚本更改为以下内容:

      $.ajax({
                type: "POST",
                async: true,
                url: 'AjaxRecieveStyle.aspx',
                data: { style: comp1Style } //as I want to pass the parameter 'style' which is internally a JSON array.
            });
我在我的C#中以以下方式获取变量style(不使用[WebServices])

我编写了一个方法
GetStyle(string-style)
来获取从ajax调用发送的数据

注意:我没有调用脚本中的AjaxReceiveStyle/GetStyle,因为在我的C方法中无法访问该方法。数据实际上是通过加载页面的方法接收的

我使用Request.Params[“style”]访问从脚本发送的变量

我的C#方法是:

protected void Page_Load(object sender, EventArgs e)
    {
        GetStyle(Request.Params["style"]);
    }

    protected void GetStyle(string style)
    {
        var recievedStyle = style;
        //do something on the recieved data!
    }

这将是一个替代任何人谁不想发送JSON数据实际上!但是使用JSON格式发送数据将提高代码的可读性

你能在你的web方法上设置一个调试中断并确保它不会被击中吗?试着将数据属性更改为data:{style:JSON.stringify(comp1Style)}@Thangadurai:我应该保持所有其他内容不变吗?试着将你的方法从“protected”更改为“public”,所有提到这个问题的人-请看我的答案!我在传递字符串数据,这就是为什么我没有提到。我将尝试发送JSON数据,并将数据类型指定为JSON:)JSON通常是一个特殊格式的数据字符串谢谢你,它成功了!我马上就发布我的答案!我稍微修改了代码请看我的答案!:如果可以的话,请告诉我在我的情况下如何传递JSON数据?我接受了你的回答,因为它使我知道我做错了什么。。请看我的答案,正如我应该做的一样。请看我的答案!请原谅我耽搁了。我现在看到您不使用WebServices,在这种情况下,挂接页面加载是可以接受的,而且非常直接。我仍然认为微软的WebAPI和严格的MVC模式是一个非常好的方法——正如我所说,在的答案提供了非常好的链接。关于使用JSON的示例,我认为这是一个非常独立的问题,应该这样讨论。同样,使用微软的MVC在这方面帮助很大。以下是您的出发点:。请注意,本文的重点是C#client+C#server,但是从C#client切换到JS client应该非常简单,因为您基于jQuery。JS代码将比C代码更简单,因为JSON对于JS来说是自然的。
protected void Page_Load(object sender, EventArgs e)
    {
        GetStyle(Request.Params["style"]);
    }

    protected void GetStyle(string style)
    {
        var recievedStyle = style;
        //do something on the recieved data!
    }