Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple function in my web service.

C#
[WebMethod]
public string TryWB()
{
    string sMsg = "try";
    return sMsg;
}


and I am calling it from my aspx page. At the top I have tried

C#
[ScriptMethod( ResponseFormat= ResponseFormat.Json)]


(or .xml or even httpget) and in the function also I have tried serialising it with

C#
sMsg = (new JavaScriptSerializer()).Serialize(sMsg);


or even made custom object just to wrap message and even tried
C#
JsonConvert.SerializeObject(cm,Newtonsoft.Json.Formatting.Indented);


From my aspx pages all combinations of text, xml, json made

JavaScript
$.ajax({
    URL: 'FLMSWebService/Login.asmx/TryWB',
    method:"POST", (get also tried)
    contentType: "application/json; charset=utf-8",
    dataType: "text",
    success: function (data) {
        alert('success');
        alert(JSON.stringify(data));
    },
    error: function (jqXHR, exception) {
        alert(jqXHR.responseText);
    }
});


I've tried: contentType: json, xml
and I've tried datatype : xml , json

I tried in web.config also

XML
<system.webserver>
<modules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

and
XML
<system.web.extensions>
<scripting>
<webservices>
<authenticationService enabled="true" />


This is all I got from my 2 day search on net...but main point if I have

dataType: "text",

It's a success and full page returned as data in every other datatype or contenttype I get error and the full page is shown in

JavaScript
alert(jqXHR.responseText);

in the error callback. I am stuck!! please help

What I have tried:

in webservice method..

tried returning a simple string as it is or used JavaScriptSerializer
or xml format (created a simple string in xml format)
or object (in this case jsonconvert.serialize)
also used scriptmethod with all responseformats, even httpget tried

web.config file - ScriptModule added and webextensions added
in scriptmanager EnablePageMethods="true" added

and in .ajax(
datatype all tried json xml text (only ttext gives success but data is full page)
contentType: json, xml - tried
Posted
Updated 3-Mar-18 1:42am
v2

In your web page do

C#
Response.Clear();
Resonse.Write (json object);
Response.Flush();
Response.End();


What is happening is the whole page is being processed. Flushing and then End the response will prevent the rest of the page from being executed and will just send back what you have put in the Response Object.
 
Share this answer
 
my web page is a webservice..can i access response there
 
Share this answer
 

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