Click here to Skip to main content
15,889,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using Jquery Ajax to call my pagemethod in asp.net. When I use
C#
contentType: "application/json; charset=utf-8",
              dataType: "json",

jquery ajax gives me error and if I dont write content type and datatye then comple page html is shown in the respose. below is my jquery ajax and .cs code:

function CallAjax()
{

$.ajax({
type: "POST",
url: "MyAdmin.aspx/GetLabelValue",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert('hi');
},
error: function(msg) { alert('fail')}
});

}

and MyAdmin.aspx.cs :

C#
[WebMethod]
  public static string GetLabelValue()
  {
      return "hello";
  }



Please guide me. Thanks !


Regards,
Divya
Posted

1 solution

Well here's the syntax of the AJAX I'm using on one of my own pages, which works fine for me:

JavaScript
var xml;
var fetchError = false;

$.ajax(
{
    type: "GET",
    contentType: "json",
    async: false,
    data: { someVar: 'some value' },
    url: "WebService.asmx/putStockplanFile",
    success: function (msg) {
        // store message returned by the request
        xml = msg;
    },
    error: function (msg) {
        // set a flag to indicate request returned an error
        fetchError = true;
    }
});

So why not give that a go first to see if it makes any difference.
 
Share this answer
 
Comments
Moykn 10-Apr-13 13:51pm    
looks like this approach doesn't works in asp.net 2.0, what happens is the server always responding with the complete page, so when you specify a dataType="json" the response fails to be validated as a json object, it seems that he will need to create a webservice to handle such calls.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900