Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
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
C#
// <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
<?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


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></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


HTML
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
Posted
Updated 13-Sep-12 2:28am
v4
Comments
Kuthuparakkal 13-Sep-12 6:34am    
what's that "error" error ?
Kartheek.chakrala 13-Sep-12 6:36am    
always it is going in to onError
Sushil Mate 13-Sep-12 7:14am    
what was the error message?
Kartheek.chakrala 13-Sep-12 7:19am    
always it is going in to onError
CodeHawkz 13-Sep-12 7:35am    
@Kartheek: They are asking what is the 'msg' you get on 'OnError()' method :)

Solution may be in the following links

1. The same type of problem is discussed Here[^].
Here he talks about JSNOP.
The last post[^] in that Question is the solution what he talks about.
Please take a look, it may help.

2. So many accepted solutions in JqueryAjax Webservice and Crossdomain problem[^].

3. Awesome article illustrating the concept - Calling Cross Domain Web Services in AJAX[^].

4. Another example - Jquery Cross-Domain ajax call using JSONP[^].

5. Nice discussion with solutions in cross domain access to service from javascript[^].

All the best.
Thanks...
 
Share this answer
 
v2
Comments
Kartheek.chakrala 20-Mar-13 5:12am    
JSONP is for Get Request we can't post data with it
derloopkat 28-Sep-13 12:00pm    
It doesn't work.
This article may help you but it's written in vb.net, use online conversion tool if you want to convert it in c#, good luck.
 
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