如何使用 jQuery 在 ASP.NET WebService 中通过 AJAX 传递数据参数

发布于 2024-12-01 10:12:51 字数 941 浏览 0 评论 0原文

当我运行不包含 parameters 的方法时,一切正常,但是当添加 parameters 运行该方法时,我收到 500 Internal Server Error 。我不确定我做错了什么,感谢您的帮助。

无法加载资源:服务器响应状态为 500(内部服务器错误)

下面是我当前使用的代码:

  [WebMethod]
    public static string UploadNewImage(string filePath,string ImageTitle,string ImageDescription,string ImageKeywords)
    {
    }
var parameters = "{'filePath':'" + fileuploadpathValue.val() + "','ImageTitle':'" +
                titleValue.val() + "','ImageDescription':'" + descriptionValue.val() + "','ImageKeywords:'" +
                    keywordsValue.val() + "'}";
 $.ajax({
                type: "POST",
                url: "../MainService.asmx/UploadNewImage",
                contentType: "application/json; charset=utf-8",
                data: parameters,
                dataType: "json",
                success: AjaxSucceeded,
                error: AjaxFailed
            });

Everything works fine when I run the method without including parameters, but when the method is run with parameters added, I get a 500 Internal Server Error. I am not sure what I am doing wrong, thanks for any help.

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Included below is the code that I am currently using:

  [WebMethod]
    public static string UploadNewImage(string filePath,string ImageTitle,string ImageDescription,string ImageKeywords)
    {
    }
var parameters = "{'filePath':'" + fileuploadpathValue.val() + "','ImageTitle':'" +
                titleValue.val() + "','ImageDescription':'" + descriptionValue.val() + "','ImageKeywords:'" +
                    keywordsValue.val() + "'}";
 $.ajax({
                type: "POST",
                url: "../MainService.asmx/UploadNewImage",
                contentType: "application/json; charset=utf-8",
                data: parameters,
                dataType: "json",
                success: AjaxSucceeded,
                error: AjaxFailed
            });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖阳 2024-12-08 10:12:51

将您的参数更改为如下:

var parameters = {
filePath: fileuploadpathValue.val(),
ImageTitle:titleValue.val(),
ImageDescription:descriptionValue.val(),
ImageKeywords:keywordsValue.val()
};

或按如下方式组合它们:

$.ajax({
                type: "POST",
                url: "../MainService.asmx/UploadNewImage",
                contentType: "application/json; charset=utf-8",
                data:  {
                    filePath: fileuploadpathValue.val(),
                     ImageTitle:titleValue.val(),
                     ImageDescription:descriptionValue.val(),
                     ImageKeywords:keywordsValue.val()
                     },
                dataType: "json",
                success: AjaxSucceeded,
                error: AjaxFailed
            });

还要确保这里的 val() 没有一个为 null,也就是说您没有在上述任何控件上设置值,如果这是 csae,您将收到错误就像“Null 传递到不接受 null 值的参数”

change your parameters to as follows:

var parameters = {
filePath: fileuploadpathValue.val(),
ImageTitle:titleValue.val(),
ImageDescription:descriptionValue.val(),
ImageKeywords:keywordsValue.val()
};

or combine them as follows:

$.ajax({
                type: "POST",
                url: "../MainService.asmx/UploadNewImage",
                contentType: "application/json; charset=utf-8",
                data:  {
                    filePath: fileuploadpathValue.val(),
                     ImageTitle:titleValue.val(),
                     ImageDescription:descriptionValue.val(),
                     ImageKeywords:keywordsValue.val()
                     },
                dataType: "json",
                success: AjaxSucceeded,
                error: AjaxFailed
            });

also make sure that none of the val() here is null that is you dint have a value set on any of the above control if that is the csae you will get an error like "Null passed into parameter which does not accept null values"

永言不败 2024-12-08 10:12:51

问题出在您的 Json 格式中,

这在我的系统中有效,

 var parameters = '{"filePath": "'+fileuploadpathValue.val()+'","ImageTitle": "'+titleValue.val()+'","ImageDescription": "'+descriptionValue.val()+'","ImageKeywords": "'+keywordsValue.val()+'"}';
        $.ajax({
            type: "POST",
            url: "../MainService.asmx/UploadNewImage",
            contentType: "application/json; charset=utf-8",
            data: parameters,
            dataType: "json",
            success: AjaxSucceeded,
            error: AjaxFailed
        });
    });

如果您需要更多信息,请告诉我

The problem is in your Json format,

This is working in my system,

 var parameters = '{"filePath": "'+fileuploadpathValue.val()+'","ImageTitle": "'+titleValue.val()+'","ImageDescription": "'+descriptionValue.val()+'","ImageKeywords": "'+keywordsValue.val()+'"}';
        $.ajax({
            type: "POST",
            url: "../MainService.asmx/UploadNewImage",
            contentType: "application/json; charset=utf-8",
            data: parameters,
            dataType: "json",
            success: AjaxSucceeded,
            error: AjaxFailed
        });
    });

Let me know if u need more in that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文