unknown web method using ajax/jquery - asp.net

ERROR: unknown web method DoIt Parameter name: methodName
I'm trying to pass a date into a DB Query function backended by VB.NET but am having problems with the webside of things.
var dat = $("#Date").val(); //textbox with a date
$.ajax({
type: "POST",
url: "file.aspx/DoIt",
cache: false,
contentType: "application/json; charset=utf-8",
data: {param:dat},
dataType: "json",
success: function (data, status) {
var response = $.parseJSON(data.d);
alert(response.message);
alert(status);
},
error: function (xmlRequest) {
alert(xmlRequest.status + ' \n\r ' + xmlRequest.statusText + '\n\r' + xmlRequest.responseText);
}
});
The file.aspx.vb file:
(at the end of the file)
<System.Web.Services.WebMethod()> _
Public Function DoIt(ByVal param As String) As String
UpdateDB(param) 'function is above
End Function
I'm just not entirely sure whats going wrong or what it means ;/

Check out this answer. You may need to declare the function as Shared
<System.Web.Services.WebMethod()> _
Public Shared Function DoIt(ByVal param As String) As String
UpdateDB(param) 'function is above
End Function

Something that might be worth checking out is to ensure that your database is setup to receive a datetime datatype.
Also something to try in your web-service declaration:
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
<WebMethod()> _
Public Function DoIt(ByVal param As String) As String
UpdateDB(param) 'function is above
End Function
Reference: webservice - unknown web method parameter name methodname

Related

json object returns nothing in WCF data service WebInvoke VB Odata

Hope someone can help. Really pulling my hair out and starting to think i should have not used wcf data services. Its been easy to get odata from the service so i thought i could send json object from my javascript code and read the contents as an object in my service But it returns nothing.
My javascript:
var vname = [];
var obj = { myobject: { frmid: "test", frmval: "1111" } }
vname.push(obj)
$.ajax({
url: "MyWCFDataService.svc/SendItems",
type: "POST",
dataType: "json",
contentType: "json",
data: { myobject: JSON.stringify(vname) },
success: function () {
alert("success :-)");
},
error: function () {
alert("fail :-(");
}
});
My class and function in my svc
<DataServiceKeyAttribute("id")> _
Public Class tobject
Public Property id As Integer
Public Property frmid As String
Public Property frmval As String
End Class
<WebInvoke()> _
Public Function SendItems(myobject As String) As Boolean
' have to ask for string as errors when asking for tobject
Return True ' nothing here yet as cannot get json object
End Function
My first venture into wcf data services and jquery. Was hoping to return a list of textboxes names and values to a wcf data service to process. Is it possible with the wcf data service?
Ok so i created a new wcf data service, removed the inherits dataservice reference and initializeservice sub.
javascript:
vname.push( { frmid: "test", frmval: "1111" })
$.ajax({
url: "MyWCFDataService.svc/SendItems",
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: { JSON.stringify(vname) },
success: function () {
alert("success :-)");
},
error: function () {
alert("fail :-(");
}
});
service:
Public Class tobject
Public Property frmid As String
Public Property frmval As String
End Class
<OperationContract>
<WebInvoke(ResponseFormat:=WebMessageFormat.Json, RequestFormat:=WebMessageFormat.Json)> _
Public Function SendItems(anyobjectname As List(Of tacosobjectitem)) As Boolean
Return True
End Function
This now turns the anyobjectname into a list of my object. I cant understand why i couldnt use BodyStyle:- Wrapped but it works so im happy. Hope it helps someone out there.

jquery ajax posting problems getting null value in alert

I'm using xml service to get response but i get null response from service.please help me solve this problem.............
By this service i am sending username and password but both wrong and right is going to succeess part because my service will return nulll value but it will be work fine in POSTMAN.........
<script>
function callXMLConnection() {
alert("call Xml method call");
var un=$("#user").val();
var pw=$("#pwd").val();
var myurl="http://192.162.1.153/EServices/retrieve.aspx";
$.support.cors = true;
$.ajax({
data :"",
type: "POST",
url:myurl,
dataType: "xml",
contentType: "application/xml; charset=utf-8",
crossDomain:true,
success: function(data, textStatus, jqXHR){
alert("text : "+data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("No data found."+jqXHR);
}
});
}
$(document).unbind('pageinit').bind('pageinit', function () {
$("#userInfo").click(function () {
callXMLConnection();
});
});
</script>
this is my get authentication method
Private Sub GenerateXMLPostMethod(ByVal Format As String, ByRef iostream As Stream)
Try
Dim WSCOMMAND As String, TMPSTR As String As String
WS_USERID = Page.Request.Item("username")
WSCOMMAND = Page.Request.Item("command")
Select Case WSCOMMAND
Case "get_authentication"
Application_Error("Responded at:" & System.DateTime.Now)
WSCOMMAND = Page.Request.Item("username")
TMPSTR = Page.Request.Item("password")
DeviceID = Page.Request.Item("deviceid")
TMPSTR = Page.Request.Item("command")&"&username"&Page.Request.Item("username") & "&password"&Page.Request.Item("username")
TMPSTR = Page.Request.Item("password") 'Passowrd
LBFN_GET_USER(WSCOMMAND, TMPSTR, iostream)
End Select
Catch ex As Exception
Application_Error("GenerateXMLPostMethod :" + ex.Message)
End Try
In code there is no data(username, password ) pass to the server, modify the your code, with
data:"" // sending null value
use like this
data:{usename:un,password:pw}
In server page you can get the values from username,password using post method

vb .net ajax call: unknown web method

I have seen a few other posts with this error, but I have tried everything suggested in those and am still having an issue.
Here is my webMethod (and class):
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class UPSImportWebServices
Inherits System.Web.Services.WebService
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
<WebMethod()> _
Public Shared Function GetInvoiceItems(ByVal invoiceId As Integer) As List(Of UPSInvoiceItem)
Return UPSInvoiceDataAccess.getInvoiceItems(invoiceId)
End Function
End Class
I just call this on document ready:
$(document).ready(function () {
$.ajax({
type: "POST",
url: '<%=ResolveUrl("~/UPSImportWebServices.asmx/GetInvoiceItems") %>',
data: { invoiceID: "22" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('worked' + data)
},
error: function (response) {
alert('error: ' + response.responseText);
},
failure: function (response) {
alert('failure: ' + response.responseText);
}
});
});
This gives me the error:
Unknown web method GetInvoiceItems. Parameter name: methodname.
EDIT: Changed invoiceID: "22" to invoiceID: 22. Still having the same issue.
I think I figured out the issue. I had copied an existing .asmx file rather than creating a new one. When I went to the url site.com/UPSImportWebServices.asmx, it showed the functions for the web service I copied. I deleted that .asmx file and created a new one and then copied my old code. It is working now.
Also, changed to '{ invoiceId:' + 22 + '}'. Note the ' and the capitalization. Rookie mistakes on this one.
Well, i think this might have to do with the type of variable you are passing. GetInvoiceItems is expecting an integer, you are passing it a string. Try doing changing this
data: { invoiceID: "22" },
to this:
data: { invoiceID: 22 },
and let me know if that works or not. If not, we can move to the next possibility.
Try changing this:
url: '<%=ResolveUrl("~/UPSImportWebServices.asmx/GetInvoiceItems") %>',
to this:
url: 'UPSImportWebServices.asmx/GetInvoiceItems',
and make sure the spelling of your asmx file is correct (case sensitive)
and lastly, try making your method just Public, not Public Shared.
Try doing this.
data: JSON.stringify({ invoiceId: 22 })
The webservice expects a JSON string.
Also, parameters are case sensitive. Your "D" in "invoiceID" is capitalized whereas the web method expects it to be "invoiceId" (lowercase d).

javascript post 500 serrver internal error

i'm using ajax and web methods and when i execute this code appear an error thar say
post POST 500 (Internal Server Error)
but this path Exist!!!
this code is i'm using in this moment
function fnSendID() {
$.ajax({
type: "POST",
url: "GMap.aspx/SendCommands",
data: '{IDMobile: "'+$("#<%=Ddl_MobileCustomer.ClientID%>").val()+'"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
console.log("Entro : " + response.d);
},
failure: function (response) {
console.log("Fallo : " + response.d);
}
});
<WebMethod()> _
Public Shared Function SendCommands(ByVal IDMobile As String) As String
'iIDMobile = GetMobileID(iIDMobile)
Dim sResponse As String = IDMobile + "Buenas"
Return sResponse
End Function
A 500 means the path you are hitting caused an error on the server side not that the path doesn't exist.
Debug the code and set a breakpoint at Dim sResponse As String = IDMobile + "Buenas".
Does it reach here? Is there any exception before the return?

converting object variable to json string for asp.net page method

This is probably a very simple task to perform but I'm taking the risk to ask anyway.
I have an object variable that looks like this:
var MyObj = {"Param1": "Default",
"Param2": "test",
"Param3": 3 };
I'm using ASP.net and I'm looking to pass this object to a page method via jquery.
So far, I have this javascript code:
function LoadObject () {
var TheObject = MyObj.toString();
$.ajax({
type: "POST",
url: "../Pages/TestPage.aspx/GetCount",
data: TheObject,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFn,
error: errorFn
});
};
I have a page method set up in the .cs file and I put a breakpoint in it but it never gets there; nothing happens.
Please let me know what changes I need to make to get this to work.
Thanks.
You need to serialize TheObject into a JSON string, and ensure that the GetCount method accepts an object with the same signature as TheObject.
I use the jQuery.JSON library to do this so that my syntax becomes:
data: "{ methodParameterName: " + $.toJSON(TheObject) + " }"
I use this library, but you can acheive the same thing with any other library in a similar manner
The first thing that you need to know is that you need to match your method name with your url
for example if your method on your code behind is named "calculate", your url must be something like this "../Pages/TestPage.aspx/calculate"
other thing that you need to keep in mind is the parameters of your method, the names and the types of your parameters must match in you ajax call and your method (code behind)
if the sign of your method is something like this
[WebMethod]
public void Calculate(string data){
// your code here
}
Your ajax call must be like this:
function LoadObject () {
var objetoJson = {
data: JSON.stringify(MyObj)
};
$.ajax({
type: "POST",
url: "../Pages/TestPage.aspx/Calculate",
data: objetoJson ,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFn,
error: errorFn
});
};
This section is so important:
var objetoJson = {
data: JSON.stringify(MyObj)
};
the name "data" is the name of your parameter in your method (code behind) and "JSON.stringify" is a helper functions already defined on your browser to convert and object to string
Hope this helps
Take a look at this thread: JSON stringify missing from jQuery 1.4.1?
Abstract: jQuery doesn't have a native method to do it. But there are many plugins out there.
EDIT
Sample C# code receiving your JSON object:
[WebMethod]
public static int GetCount(GetCountParams p)
{
// ... Do something with p.Param1, p.Param2, etc.
return 0;
}
public class GetCountParams
{
public string Param1 { get; set; }
public string Param2 { get; set; }
public string Param3 { get; set; }
}
EDIT 2
Sample jQuery AJAX call using that object as parameter:
$.ajax({
type: "POST",
url: "../Pages/TestPage.aspx/GetCount",
data: "{ p: '" JSON.stringify(MyObj) + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json"
});

Resources