问题
I want to create a web method that accepts a List of custom objects (passed in via jQuery/JSON).
When I run the website locally everything seems to work. jQuery and ASP.NET and everyone is happy. But when I put it on one of our servers it blows up. jQuery gets a 500 error after the ajax request with the response being:
System.InvalidOperationException: EditCustomObjects Web Service method name is not valid.
Here's the web service method:
[WebMethod]
public void EditCustomObjects(int ID, List<CustomObject> CustomObjectList)
{
// Code here
}
And my jQuery code (which I don't think matters, since the error seems to be happening on the web service level):
var data = JSON.stringify({
ID: id,
CustomObjectList: customObjectList
});
$.ajax({
type: "POST",
url: "/manageobjects.asmx/EditCustomObjects",
data: data,
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function(xml, ajaxStatus) {
// stuff here
}
});
The customObjectList is initialized like so:
var customObjectList = [];
And I add items to it like so (via a loop):
var itemObject = {
ObjectTitle = objectTitle,
ObjectDescription = objectDescription,
ObjectValue = objectValue
}
customObjectList.push(itemObject);
So, am I doing anything wrong here? Is there a better way of passing an array of data from jQuery to an ASP.NET web service method? Is there a way to resolve the "Web Service method name is not valid." error?
FYI, I am running .NET 2.0 on a Windows Server 2003 machine, and I got the code for the above from this site: http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/
EDIT: Someone requested some more info on the web service, I'd rather not provide the whole class but here is a bit more that may help:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ManageObjects : Custom.Web.UI.Services.Service
{
}
Bara
回答1:
I make the assuption based on comments that you can directly go to the web service in the browser.
Just to isolate your custom object from configuration, you could put another service in place like:
[WebMethod]
public static string GetServerTimeString()
{
return "Current Server Time: " + DateTime.Now.ToString();
}
Call that from a client side jQuery ajax call. If this works, then it is probably related to your object specifically and not configuration on the server side. Otherwise, keep looking on the server side config track.
EDIT: Some sample code:
[WebMethod(EnableSession = true)]
public Category[] GetCategoryList()
{
return GetCategories();
}
private Category[] GetCategories()
{
List<Category> category = new List<Category>();
CategoryCollection matchingCategories = CategoryList.GetCategoryList();
foreach (Category CategoryRow in matchingCategories)
{
category.Add(new Category(CategoryRow.CategoryId, CategoryRow.CategoryName));
}
return category.ToArray();
}
And here is an example of where I post a complex data type JSON value
[WebMethod]
public static string SaveProcedureList(NewProcedureData procedureSaveData)
{
...do stuff here with my object
}
This actually includes two arrays of objects inside it... my NewProcedureData type is defined in a class which lays those out.
EDIT2:
Here is how I handle a complex object in one instance:
function cptRow(cptCode, cptCodeText, rowIndex)
{
this.cptCode = cptCode;
this.cptCodeText = cptCodeText;
this.modifierList = new Array();
//...more stuff here you get the idea
}
/* set up the save object */
function procedureSet()
{
this.provider = $('select#providerSelect option:selected').val(); // currentPageDoctor;
this.patientIdtdb = currentPatientIdtdb;// a javascript object (string)
//...more object build stuff.
this.cptRows = Array();
for (i = 0; i < currentRowCount; i++)
{
if ($('.cptIcdLinkRow').eq(i).find('.cptEntryArea').val() != watermarkText)
{
this.cptRows[i] = new cptRow($('.cptIcdLinkRow').eq(i).find('.cptCode').val(), $('.cptIcdLinkRow').eq(i).find('.cptEntryArea').val(), i);//this is a javscript function that handles the array object
};
};
};
//here is and example where I wrap up the object
function SaveCurrentProcedures()
{
var currentSet = new procedureSet();
var procedureData = "";
var testData = { procedureSaveData: currentSet };
procedureData = JSON.stringify(testData);
SaveProceduresData(procedureData);
};
function SaveProceduresData(procedureSaveData)
{
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: procedureSaveData,
the rest of the ajax call...
});
};
NOTE !IMPORTANT the procedureSaveData name must match exactly on the client and server side for this to work properly. EDIT3: more code example:
using System;
using System.Collections.Generic;
using System.Web;
namespace MyNamespace.NewProcedure.BL
{
/// <summary>
/// lists of objects, names must match the JavaScript names
/// </summary>
public class NewProcedureData
{
private string _patientId = "";
private string _patientIdTdb = "";
private List<CptRows> _cptRows = new List<CptRows>();
public NewProcedureData()
{
}
public string PatientIdTdb
{
get { return _patientIdTdb; }
set { _patientIdTdb = value; }
}
public string PatientId
{
get { return _patientId; }
set { _patientId = value; }
}
public List<CptRows> CptRows = new List<CptRows>();
}
--------
using System;
using System.Collections.Generic;
using System.Web;
namespace MyNamespace.NewProcedure.BL
{
/// <summary>
/// lists of objects, names must match the JavaScript names
/// </summary>
public class CptRows
{
private string _cptCode = "";
private string _cptCodeText = "";
public CptRows()
{
}
public string CptCode
{
get { return _cptCode; }
set { _cptCode = value; }
}
public string CptCodeText
{
get { return _cptCodeText; }
set { _cptCodeText = value; }
}
}
}
Hope this helps.
回答2:
Since the error mentions a problem with the web method name, I suspect this is entirely due to problems in how the WSDL is generated and mapped to the web service. I see two potential problems:
ID
is an awfully common name and may be reserved or may cause conflicts. Try renaming it toobjectId
(capitalized parameter names are not the norm for .NET anyway)- WSDL does not have a concept of generic types. Depending on the generator, the type name generated for
List<CustomObject>
orCustomObject[]
may conflict with your parameter nameCustomObjectList
. Try renaming your second parametercustomObjects
instead.
Or maybe there's some other silly framework issues at play here. In any case, you should add to your question the WSDL generated by .NET. You can get the WSDL XML by downloading /manageobjects.asmx?WSDL from your web server.
回答3:
You may want to check your webServices/protocols element in the system.web section and ensure that HttpPost is added. Typically, local post is enabled by default as development mode but not for remote, which is what it will be when the service is deployed on the server...
<system.web>
...
<webServices>
<protocols>
<add name="HttpPost" />
</protocols>
</webServices>
</system.web>
I go into more detail here
回答4:
Maybe the path you are passing with jquery does not exist try to see the response using FireBug.
回答5:
Whenever I get these kinds of errors (working on local but not on server) I fire up IIS admin tool (or whatever you are using) and check that everything is EXACTLY the same. If not make them the same then change 1 little thing at a time.
Depending on your role/organisation you might not have access to the server in this way but often these errors come down to the weirdest random checkbox on a tab you never look at.
It is a long shot but can't help to try.
回答6:
Do you have the required httpHandler registered for allowing webservices marked as a ScriptService?
<httpHandlers>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</httpHandlers>
回答7:
This is a wrong declaration in javascript.
var itemObject = {
ObjectTitle = objectTitle,
ObjectDescription = objectDescription,
ObjectValue = objectValue
}
Correct object initialization should be:
var itemObject = {
ObjectTitle : objectTitle,
ObjectDescription : objectDescription,
ObjectValue : objectValue
};
Also try temporary removing async: false
from $.ajax
's parameters.
回答8:
Have to say, I am not 100% sold my answer is (all) the issue, BUT this would address the deserialization. Adding some detail for your specific object.
on client side:
function itemObject (objectTitle, objectDescription ,objectValue )
{
this.ObjectTitle = objectTitle,;
this.ObjectDescription = objectDescription ;
this.ObjectValue = objectValue ;
};
// 4 objects in source-fix for your list
function objRow()
{
this.myCode = myCode;
this.myCodeText = myCodeText;
this.customObjectList = new Array();
for (k = 0; k < 4; k++)
{
this.customObjectList [k] = new itemObject (sourceTitle[k],sourceDescription[k],sourceValue[k]);
};
};
var myCode = "hicode1";
var myCodeText = "hicodeText1";
$(function()
{
function SaveCurrentObjects()
{
var currentSet = new objRow();
var objectData = "";
var testData = {objectSaveData : currentSet };
objectData = JSON.stringify(testData);
SaveObjectsData(objectData );
};
/* save the objects */
function SaveObjectsData(objectSaveData)
{
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: objectSaveData,
dataFilter: function(data)
{
var msg;
if (typeof (JSON) !== 'undefined' &&
typeof (JSON.parse) === 'function')
msg = JSON.parse(data);
else
msg = eval('(' + data + ')');
if (msg.hasOwnProperty('d'))
return msg.d;
else
return msg;
},
url: "/manageobjects.asmx/EditCustomObjects",
success: function(msg)
{
//do stuff
},
failure: function(msg)
{
//handlefail
}
});
};
});
on server side you would have:(exact name of "objectSaveData") as in client code.
[WebMethod]
public static string EditCustomObjects(ObjectData objectSaveData)
{
// code to save your object
return "saved objects";
}
create a ObjectData class cointaining a list of objectItem similar to my other example matching up the objects (careful of names).
NOTE: I have the ASP.NET ajax extensions installed - working with asp.net 2.0 code for this example.
回答9:
Alright, here's how it is for an object, just tested this: ============================================ASPX==================================
$("#btngeMethodCallWithAnObjectAsAParameter").click(function (event) {
$.ajax({
type: 'POST',
url: 'Default.aspx/PageMethodCallWithAnObjectAsAParameter',
data: '{"id":"' + '5' + '", "jSonAsset":' + JSON.stringify(new BuildJSonAsset()) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d) {
alert(msg.d);
}
},
error: function () {
alert("Error! Try again...");
}
});
});
});
function BuildJSonAsset() {
this.AssetId = '100';
this.ContentId = '200';
this.AssetName = 'Asset1';
}
========================================================================
[WebMethod()]
public static string PageMethodCallWithAnObjectAsAParameter(int id, JSonAsset jSonAsset)
{
return "Success!";
}
======================================================================
And this works for a single object, next I am going to try a list of objects:)
回答10:
Ensure you have this at the top of your class
[ScriptService] <--- this bit :D
public class Surveyors : WebService
Also check all of your parathesis, resharper went a bit crazy on me and it took a while to check through.
Also if you have many webmethods, try to make a basic hello world webservice, make sure its actually working in the first place then you can troubleshoot from there.
Good Luck.
来源:https://stackoverflow.com/questions/2225781/asp-net-web-method-that-accepts-a-listcustomobject-is-failing-with-web-servic