Ajax调用 - 简单的Web服务方法返回整个页面 [英] Ajax call - simple web service method returns entire page

查看:98
本文介绍了Ajax调用 - 简单的Web服务方法返回整个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网络服务功能很简单。



I have a simple function in my web service.

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





我从我的aspx页面调用它。在顶部,我尝试了





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

[ScriptMethod( ResponseFormat= ResponseFormat.Json)]





(或.xml甚至是httpget),在功能中我也试过用





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

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





甚至制作自定义对象只是为了包装消息甚至尝试过



or even made custom object just to wrap message and even tried

JsonConvert.SerializeObject(cm,Newtonsoft.Json.Formatting.Indented);





从我的aspx页面,所有文本组合,xml,json制作





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

$.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);
    }
});





我试过了:contentType:json,xml

我尝试过数据类型:xml,json



我在web.config中试过了





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

I tried in web.config also

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






and

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





这是我在网上搜索2天所得到的全部内容......但如果我有主要观点



dataType:text,



这是成功的,并且在每个其他数据类型或内容类型I中返回完整页面作为数据得到错误,整页显示在





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

alert(jqXHR.responseText);


错误回调中的
。我卡住了!!请帮助



我尝试过:



webservice方法。 。



尝试返回一个简单的字符串,或者使用JavaScriptSerializer

或xml格式(以xml格式创建一个简单的字符串)

或对象(在本例中为jsonconvert.serialize)

也使用了scriptmethod和所有的响应格式,甚至httpget尝试了



web.config文件 - 添加了ScriptModule并添加了webextensions

in scriptmanager EnablePageMethods =true添加



和.ajax(

数据类型全部试过json xml文本(只有ttext成功但数据是整页)

contentType:json,xml - 试过


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

推荐答案

.ajax({
URL:' FLMSWebService / Login.asmx / TryWB'
方法: POST,(也试过)
contentType: application / json; charset = utf-8
dataType: text
成功: function (数据){
alert(' 成功);
alert( JSON .stringify(data));
},
错误: function (jqXHR,exception){
alert(jqXHR.responseText);
}
});
.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); } });





我试过了:contentType:json,xml

我尝试过数据类型:xml,json



我在web.config中试过了





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

I tried in web.config also

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






and

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





这是我在网上搜索2天所得到的全部内容......但如果我有主要观点



dataType:text,



这是成功的,并且在每个其他数据类型或内容类型I中返回完整页面作为数据得到错误,整页显示在





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

alert(jqXHR.responseText);


错误回调中的
。我卡住了!!请帮助



我尝试过:



webservice方法。 。



尝试返回一个简单的字符串,或者使用JavaScriptSerializer

或xml格式(以xml格式创建一个简单的字符串)

或对象(在本例中为jsonconvert.serialize)

也使用了scriptmethod和所有的响应格式,甚至httpget尝试了



web.config文件 - 添加了ScriptModule并添加了webextensions

in scriptmanager EnablePageMethods =true添加



和.ajax(

数据类型全部试过json xml文本(只有ttext成功但数据是整页)

contentType:json,xml - 试过


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


在你的网页确实



In your web page do

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





正在发生的是整个p年龄正在处理中。刷新然后结束响应将阻止页面的其余部分被执行,并将发回您放在响应对象中的内容。



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.


我的网页是webservice..can我在那里访问响应
my web page is a webservice..can i access response there


这篇关于Ajax调用 - 简单的Web服务方法返回整个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆