Click here to Skip to main content
13,798,638 members
Rate this:
 
Please Sign up or sign in to vote.
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
Posted 13-Sep-12 1:30am
Updated 13-Sep-12 3: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 :)
Kartheek.chakrala 13-Sep-12 7:40am
   
Undefined only i am getting nothing any message
ZurdoDev 13-Sep-12 8:32am
   
msg.d IS undefined in the OnError event. From http://api.jquery.com/jQuery.ajax/ the error signature is error(jqXHR, textStatus, errorThrown). Examine those values and you'll see the error. Look at jqXHR.responseText.
Kartheek.chakrala 13-Sep-12 8:57am
   
i found the problem but not the solution
Problem is Cross Domain Webservice Call

solution till now dnt know. If any body have simple solution ?
darshan_ur 13-Sep-12 10:09am
   
IF the problem is with cross domain, then you could try adding a crossdomain.xml file in your asmx folder to allow cross domain access.
Examples of crossdomain.xml are given in below link:
http://www.senocular.com/pub/adobe/crossdomain/policyfiles.html
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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...
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.181207.3 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100