Reputation: 3515
function sendStates(data) {
var states = new Object();
states.Id = 1;
states.Name = "Chandan";
$.post("WebService.asmx/MultiDim/", states, function (data) {
alert("reached");
});
}
sendStates(null);
WebService file:
[WebMethod]
public void MultiDim(statesTemplate states)
{
}
MultiDim function doesnt get called.
Upvotes: 0
Views: 101
Reputation: 1662
Do your JS code and WS share the same path? Try specifying the path to the webservice, and I included an easier initialization of the object.
function sendStates(data) {
var states = {Id: 1, Name: "Chandan"};
$.post("/WebService.asmx/MultiDim/", states, function (data) {
alert("reached");
});
}
UPDATE: Have you watched the network traffic to verify that a request is actually being sent to the service, or watched in the console for any post feedback?
I implemented the following on test application (minus the webservice) with no issues at all:
function dataTest (data) {
console.info('Arrived in the dataTest function');
var states = { Id: 1, Name: 'Dustin' };
$.post("Home/SomeTest", states, function (data) {
console.info('Post complete, data below');
console.log(data);
alert('here');
}, 'json');
}
var ob = [{ Id: 3, Name: "Test" }, { Id: 4, Name: "Testing"}];
dataTest(ob);
UPDATE 2: Were you able to verify that function is actually getting called, via console or network? Toss in a console.info('some text here') to determine if you are even making it into your function. If you are making it into your function, the problem lies in the WebService. If you are not making it to the function, the problem lies in your JS.
Update 3: I would begin by verifying that you can call the WebService with the address you posted, and that it functions as expected. If that passes: If the call is the result of an event and it's corresponding handler: I would begin by tracing from the event handler, verifying that it is indeed being triggered, and finally that it is making it to the sendStates(data) call. Else: Set breakpoints and verify that you are making to your sendStates(data) call. Google Chrome makes Javascript debugging a walk in the park, if you aren't already using it. Best of luck!
Upvotes: 1
Reputation: 82624
Change:
var states = new Object();
states.Id = 1;
states.Name = "Chandan";
to
var object = {states: {Id: 1, Name: "Chandan" }}
$.post("WebService.asmx/MultiDim/", object, function (data) {
alert("reached");
});
The method parameter name is needed. The object you send to .NET needs the method parameter name in order to parse your Request.
[WebMethod]
public void MultiDim(statesTemplate states /* must be the javascript name too */)
{
}
jQuery doesn't know that your object's variable name is states
. It is only an object with two properties Id
and Name
.
So adding an object with a property states
is the answer.
var objectToSendToNet = new Object();
objectToSendToNet.states = new Object();
objectToSendToNet.states.Id = 1;
objectToSendToNet.states.Name = "Chandan";
$.post("WebService.asmx/MultiDim/", objectToSendToNet, function (data) {
alert("reached");
});
Upvotes: 1
Reputation: 36947
is it making a post to the url specified? Is there any distinct javascript errors? So far I see 1 possible issue. Your not specifying a callback ie: json, xml, other which would be defined after the function(){}
part of the post but within the ()
. So it may be ignoring and output and skipping over the function within the post to do something with data being sent back.
example of how I might change it to see if its working or not.
function sendStates(data) {
var states = new Object();
states.Id = 1;
states.Name = "Chandan";
$.post("WebService.asmx/MultiDim/", states, function (data) {
alert("reached");
}, 'json');
}
if your not already using it, I would also suggest getting a browser plugin called firebug. It can help you determine many of potential issues you may be having otherwise as well.
Upvotes: 0