简体   繁体   English

URL中有参数时将数据发送到服务器

[英]Sending data to server when having parameters in URL

Im having trouble with sending a message to a WebMethod 我在向WebMethod发送消息时遇到麻烦

The piece is in a ascx so the URL is the parent page or else there is only a 404 该片段位于一个ascx中,因此URL是父页面,否则只有404

i have the JQ code 我有JQ码

$("#saveCanvas").click(function () {
        var image = document.getElementById("SolutionDisplay").toDataURL("image/png");
        image = image.replace('data:image/png;base64,', '');

        $.ajax({
            type: 'POST',
            url: "Student.aspx/UploadImage",
            data: '{ "imageData" : "' + image + '" }',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (msg) {
                alert('Image saved successfully !');
            }
        });
});

called from the button 从按钮调用

<input type="button" id="saveCanvas" name="saveCanvas" value="Save Image" />

but i keep getting 500 errors, like this 但我不断收到500个错误,像这样

POST http://localhost:83/Student.aspx/UploadImage 500 (Internal Server Error) jquery-1.10.2.js:8720
    send                                jquery-1.10.2.js:8720
    jQuery.extend.ajax                  jquery-1.10.2.js:8150
    (anonymous function)                Student.aspx?Page=Assignment&&CourseID=14:709
    jQuery.event.dispatch               jquery-1.10.2.js:5109
    elemData.handle                     jquery-1.10.2.js:4780

Could it have something to do with the GET url being? 可能与GET网址有关吗?

http://localhost:83/Student.aspx?Page=Assignment&&CourseID=14

the server code should be straightforward 服务器代码应该简单明了

on the class there is a 在课堂上有一个

[ScriptService]

and in the class there is the "handler" 在课堂上有“处理者”

static string PathTest = @"D:\";
[WebMethod()]
public static void UploadImage(string imageData)
{
    string fileNameWitPath = PathTest + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";
    using (FileStream fs = new FileStream(fileNameWitPath, FileMode.Create))
    {
        using (BinaryWriter bw = new BinaryWriter(fs))
        {
            byte[] data = Convert.FromBase64String(imageData);
            bw.Write(data);
            bw.Close();
        }
    }
}

I solved it. 我解决了 the problem was running the ajax under a usercontroll. 问题是在用户控制下运行ajax。 moved the methods out to the parent aspx and it ran beautifully. 将方法移到父aspx上,并且运行精美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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