Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a very simple asmx webservice in one Visual Studio project
C#
[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string HelloWorld()
        {
            return "helloworld";
        }

    }


I am trying to call this web service from another project in Visual Studio using ajax call

JavaScript
$(document).ready(function () {

            $.ajax({
                url: "http://localhost:17889/Service1.asmx/HelloWorld",
                type: "POST",
                dataType: "jsonp",
                crossDomain:true,
                contentType: "application/json;charset=utf-8",
                
                success: function (d) {

                    alert("success");
                    alert(d.data);

                },
                error: function (xhr, status, error) {


                    console.log(xhr.responseText);
                    console.log(status);
                    console.log(error);
                   
                }
            });

        });



my web.config looks like this
XML
<webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
      </webServices>


XML
<httpProtocol>
       <customHeaders>
         <add name="Access-Control-Allow-Origin" value="http://localhost:17889" />
         <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
       </customHeaders>

     </httpProtocol>




the errors are
Uncaught SyntaxError: Unexpected token <<br mode="hold" />
JavaScript
undefined

parsererror
Error: jQuery19004053758140653372_1453581966937 was not called(…)

what's wrong with my approach? I have gone through a lot of posts, but still struggling to get a proper solution.
Please suggest.
Posted
Comments
[no name] 24-Jan-16 0:42am    
Go through below links that may help you:

http://codewala.net/2013/02/12/accessing-other-domain-web-services-via-jquery-ajax-cors/

http://www.codeproject.com/Articles/667387/Jquery-Cross-Domain-ajax-call-using-JSONP
swayamrath 25-Jan-16 11:37am    
Thanks Manas.
I tries every possible option given in the blogs. But still no success.

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