无法通过jquery ajax调用asmx Web服务 [英] unable to call asmx web service through jquery ajax

查看:100
本文介绍了无法通过jquery ajax调用asmx Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个asmx Web服务并在localhost:5739上运行并在端口外创建了一个html页面,并尝试从jquery调用Web服务。我总是进入onError函数并显示undefind;







asmx web sercie is

  //  < summary>  
/// testservice的摘要说明
/// < / summary >
[WebService(Namespace = http://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
/ / 要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class testservice:System.Web.Services.WebService {

public testservice(){

// 如果使用设计的组件,则取消注释以下行
// InitializeComponent();
}

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld( string text){
return Hello World + text;
}

}





asmx网络服务网站。 config是



 <?  xml     version   =  1.0  >  
<! -
更多信息关于如何配置ASP.NET应用程序,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
- >

< 配置 >
< system.web >
& lt; 编译 debug = true targetFramework = 4.0 / >
< / system.web >
< / configuration >









Jquery网络服务电话mwthod是





< pre lang =HTML> < !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< html xmlns = http://www.w3.org/1999/xhtml >
< head >
< title < span class =code-keyword>> < / title >
< script type = text / javascript src = ../ JS / jquery-1.8.1.js > < span class =code-keyword>< / script >
< script type = text / javascript >

$( document ).ready( function (){
$(' #btn_test')。click( function (){
debugger ;
var name = Kartheek;
$ .ajax(
{
type: POST
url: http:// localhost:5739 / asmxservices / testservice.asmx / HelloWorld
data:' {text:Kartheek}'
contentType: application / json; charset = utf-8
dataType : json
成功:OnSuccess,
错误:OnError

});

function OnSuccess(数据,状态){
alert(data.d);

};
function OnError(msg){
alert(' error =' + msg.d);
}

});


});
< / script >
< / head >
< body >
< p >
< 输入 id = btn_test type = 提交 value = testservice / > < / p >
< / body >
< / html >









In Inspect元素标题是





 21ms1.04s 

HeadersPreviewResponseCookiesTiming
请求URL:http:// localhost:5739 / asmxservices / testservice.asmx / HelloWorld
请求方法:选项
状态代码:500内部服务器错误
请求标题查看源
接受:* / *
接受 - 字符集:ISO-8859-1,utf-8; q = 0.7,*; q = 0.3
接受编码:gzip,deflate,sdch
接受语言:en-US,en; q = 0.8
Access-Control-Request-Headers:origin,content-type,accept
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost: 5739
来源:null
用户代理:Mozilla / 5.0(Windows NT 6.1)AppleWebKit / 537.1(KHTML,类似Gecko)Chrome / 21.0.1180.89 Safari / 537.1
响应标题查看源
Cache-Control:private
Connection:Close
Content-Length:4850
Content-Type:text / html; charset = utf-8
日期:2012年9月13日星期四10:24:52 GMT
服务器:ASP.NET开发服务器/ 10.0.0.0
X-AspNet-版本:4.0.30319

解决方案

document )。ready( function (){


' # btn_test')。点击( function (){
debugger ;
var name = Kartheek ;


.ajax(
{
type: POST
url: http:// localhost:5739 / asmxservices / testservice .asmx / HelloWorld
数据:' {text:Kartheek}'
contentType: application / json; charset = utf-8
dataType: json
成功:OnSuccess,
错误:OnError

});

function OnSuccess(数据,状态){
alert(data.d);

};
function OnError(msg){
alert(' error =' + msg.d);
}

});


});
< / script >
< / head >
< body >
< p >
< span class =code-keyword>< input id = btn_test type = submit value = testservice / > < / p >
< / body >
< / html >









In Inspect元素标题是





 21ms1.04s 

HeadersPreviewResponseCookiesTiming
请求URL:http:// localhost:5739 / asmxservices / testservice.asmx / HelloWorld
请求方法:选项
状态代码:500内部服务器错误
请求Headersview源
接受:* / *
Accept-Charset:ISO-8859-1,utf-8; q = 0.7,*; q = 0.3
Accept-Encoding:gzip, deflate,sdch
Accept-Language:en-US,en; q = 0.8
Access-Control-Request-Headers:origin,content-type,accept
Access-Control-Request-Method :POST
连接:keep-alive
主机:localhost:5739
来源:null
用户代理:Mozill a / 5.0(Windows NT 6.1)AppleWebKit / 537.1(KHTML,类似Gecko)Chrome / 21.0.1180.89 Safari / 537.1
响应Headersview源
Cache-Control:private
连接:关闭
内容长度:4850
内容类型:text / html; charset = utf-8
日期:2012年9月13日星期四10:24:52 GMT
服务器:ASP.NET开发服务器/ 10.0.0.0
X-AspNet-版本:4.0.30319


i created a asmx web service and running on localhost:5739 and created a html page out side of the port and trying to call web service from jquery. i am always it is going in to onError function and displaying undefind;



asmx web sercie is

// <summary>
/// Summary description for testservice
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class testservice : System.Web.Services.WebService {

    public testservice () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string HelloWorld(string text) {
        return "Hello World" + text;
    }
    
}



asmx web service web. config is

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
</configuration>





Jquery web service call mwthod is


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="../JS/jquery-1.8.1.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {
            $('#btn_test').click(function () {
                debugger;
                var name = "Kartheek";
                $.ajax(
                {
                    type: "POST",
                    url: "http://localhost:5739/asmxservices/testservice.asmx/HelloWorld",
                    data: '{ "text": "Kartheek"}',                    
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    error: OnError

                });

                function OnSuccess(data, status) {
                    alert(data.d);

                };
                function OnError(msg) {
                    alert('error = ' + msg.d);
                }

            });


        });
    </script>
</head>
<body>
    <p>
        <input id="btn_test" type="submit" value="testservice" /></p>
</body>
</html>





In Inspect element headers are


21ms1.04s

HeadersPreviewResponseCookiesTiming
Request URL:http://localhost:5739/asmxservices/testservice.asmx/HelloWorld
Request Method:OPTIONS
Status Code:500 Internal Server Error
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:origin, content-type, accept
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:5739
Origin:null
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
Response Headersview source
Cache-Control:private
Connection:Close
Content-Length:4850
Content-Type:text/html; charset=utf-8
Date:Thu, 13 Sep 2012 10:24:52 GMT
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319

解决方案

(document).ready(function () {


('#btn_test').click(function () { debugger; var name = "Kartheek";


.ajax( { type: "POST", url: "http://localhost:5739/asmxservices/testservice.asmx/HelloWorld", data: '{ "text": "Kartheek"}', contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, error: OnError }); function OnSuccess(data, status) { alert(data.d); }; function OnError(msg) { alert('error = ' + msg.d); } }); }); </script> </head> <body> <p> <input id="btn_test" type="submit" value="testservice" /></p> </body> </html>





In Inspect element headers are


21ms1.04s

HeadersPreviewResponseCookiesTiming
Request URL:http://localhost:5739/asmxservices/testservice.asmx/HelloWorld
Request Method:OPTIONS
Status Code:500 Internal Server Error
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:origin, content-type, accept
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:5739
Origin:null
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
Response Headersview source
Cache-Control:private
Connection:Close
Content-Length:4850
Content-Type:text/html; charset=utf-8
Date:Thu, 13 Sep 2012 10:24:52 GMT
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319


这篇关于无法通过jquery ajax调用asmx Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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