1

什么会导致这个?

Error System.InvalidOperationException: UpdateProject Web Service method name is not valid.

   at System.Web.Services.Protocols.HttpServerProtocol.Initialize()

   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

JavaScript 代码:

                Ext.Ajax.request({
                        url: 'http://localhost/Controls/ProjectList/ProjectListService.asmx/UpdateProject',
                        params: {
                            'project': { project_id: 'a', project_number: 'b', project_name: 'c' }
                        },
                        success : function(response){
                            alert(response.responseText);
                        }, 
                        failure : function(response){
                            alert("Error " + response.responseText);
                        }
                });                         

网络服务(ProjectListService.asmx):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Data;
using System.Web.Script.Services;
using System.IO;
using System.Text;

namespace Web.Controls.ProjectList
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class ProjectListService : System.Web.Services.WebService
    {
        [Serializable]
        public class Project
        {
            public string project_id;
            public string project_number;
            public string project_name;
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json,
            UseHttpGet = false, XmlSerializeString = false)]
        public List<Project> GetProjects(string myTest, string bar)
        {
            var list = new List<Project>(new[] {
                new Project() {project_id="1", project_name="project 1", project_number="001"},
                new Project() {project_id="2", project_name= "project 2", project_number= "002" },
                new Project() {project_id="3", project_name= "project 3", project_number= "003" }
            });

            return list;
        }

        [WebMethod]
        [ScriptMethod]
        //[ScriptMethod(ResponseFormat = ResponseFormat.Json,
        //    UseHttpGet = false, XmlSerializeString = false)]
        public void UpdateProject(Project project)
        {

            string x = "";
            Project p = project;

        }
    }
}

===========================

2012 年 7 月 20 日 @ 10:43 更新:

新错误:

Error {"Message":"Invalid JSON primitive: project.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n   at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

JavaScript 代码:

                Ext.Ajax.request({
                        url: 'http://localhost/Controls/ProjectList/ProjectListService.asmx/UpdateProject',
                    headers: { 'Content-Type': 'application/json' },
                    method: 'POST',
                    scope: this,
                        params: {
                            'project': { project_id: 'a', project_number: 'b', project_name: 'c' }
                        },
                        success : function(response){
                            alert(response.responseText);
                        }, 
                        failure : function(response){
                            alert("Error " + response.responseText);
                        }
                });  

====================================

2012 年 7 月 20 日下午 2:40 更新:

工作 JSON 请求:

                var jsonStr = Ext.encode({ 'project_id': 'a', 'project_number': 'b', 'project_name': 'c' });

                Ext.Ajax.request({
                        url: 'http://localhost/Controls/ProjectList/ProjectListService.asmx/SaveProject',
                    method: 'POST',
                        jsonData: jsonStr,
                        success : function(response){
                            alert(response.responseText);
                        }, 
                        failure : function(response){
                            alert("Error " + response.responseText);
                        }
                });  

网络方法(到目前为止):

(请参阅上面有问题的项目对象)

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = false)]
public string SaveProject()
{         
    return "success";
}

=================================

2012 年 7 月 22 日 @ 晚上 9:03 更新(已修复):

改变了两件事。为我的 Web 服务添加了“项目”类型的“项目”参数。然后将项目对象属性放在我的“jsonData”对象中该对象的内部。

工作 JSON 请求:

            //var jsonStr = Ext.encode({ 'project_id': 'a', 'project_number': 'b', 'project_name': 'c' });
            var jsonDataObject = { 'project': { 'project_id: '4', 'project_number': '004', 'project_name': 'project 4' } };

            Ext.Ajax.request({
                        url: 'http://localhost/Controls/ProjectList/ProjectListService.asmx/SaveProject',
                method: 'POST',
                        jsonData: jsonDataObject,
                        success : function(response){
                            alert(response.responseText);
                        }, 
                        failure : function(response){
                            alert("Error " + response.responseText);
                        }
            });  

网络方法(到目前为止):

(请参阅上面有问题的项目对象)

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = false)]
public string SaveProject(Project project)
{         
    return "success";
}
4

1 回答 1

2

看起来序列化程序认为您不能将对象作为 JSON 节点的属性。

几件事要尝试:

  • 由于 Project 是 UpdateProject 函数的唯一参数,您可以将您的项目作为数据传递吗?params: { project_id: 'a', project_number: 'b', project_name: 'c' }
  • 尝试使用jsonData而不是“参数” 。这也将使您不需要自己添加 application/json 标头,并且序列化可能会正确发生(如果问题出在客户端上)。
于 2012-07-20T18:50:13.247 回答