Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I've an autocomplete textbox, Searching is working fine based on what we type in the textbox. If i write the search method in the .cs file working fine. but the same i write in .asmx file, raising the following error.

What is the problem?

JSON response :

VB
Message
    "Authentication failed."

StackTrace
    null

ExceptionType
    "System.InvalidOperationException"
"NetworkError: 500 Internal Server Error - http://localhost:51636/ProjectName/AutoComplete.asmx/SearchCities"
Posted
Updated 3-Sep-18 18:41pm
Comments
Bernhard Hiller 21-Oct-13 9:55am    
"in the .cs file working fine" - does that mean: when you run the code in a console application or Windows application instead of IIS or other web server?
virang_21 21-Oct-13 17:27pm    
Try to catch inner exception when returning JSON to see what exactly is failing..

catch (WebException wex)
{
result = ((HttpWebResponse)wex.Response).StatusDescription;

}
Rockstar_ 24-Oct-13 1:29am    
The error is :

{"Message":"Authentication failed.","StackTrace":null,"ExceptionType":"System.InvalidOperationException"}
MairajAhmed 5-Sep-14 8:27am    
Please see console of browser you will see error.

This error is thrown because you are trying to call a method of another domain. this is restricted from browsers for security purpose.
here you can do this through CORS(new functionality in HTML5).
set following property in your javascript.
JavaScript
$.support.cors = true;

Learn about CORS from here...
http://www.html5rocks.com/en/tutorials/cors/[^]

And other solution is to use jsonp dataType to call your service.

here is good link to know about jsonp dataType.
http://www.jquery4u.com/json/jsonp-examples/[^]
 
Share this answer
 
v2
 
Share this answer
 
Use below code for ajax call, It's working fine...
$.ajax({

  // The 'type' property sets the HTTP method.
  // A value of 'PUT' or 'DELETE' will trigger a preflight request.
  type: 'GET',

  // The URL to make the request to.
  url: 'http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html',

  // The 'contentType' property sets the 'Content-Type' header.
  // The JQuery default for this property is
  // 'application/x-www-form-urlencoded; charset=UTF-8', which does not trigger
  // a preflight. If you set this value to anything other than
  // application/x-www-form-urlencoded, multipart/form-data, or text/plain,
  // you will trigger a preflight request.
  contentType: 'text/plain',

  xhrFields: {
    // The 'xhrFields' property sets additional fields on the XMLHttpRequest.
    // This can be used to set the 'withCredentials' property.
    // Set the value to 'true' if you'd like to pass cookies to the server.
    // If this is enabled, your server must respond with the header
    // 'Access-Control-Allow-Credentials: true'.
    withCredentials: false
  },

  headers: {
    // Set any custom headers here.
    // If you set any non-simple headers, your server must include these
    // headers in the 'Access-Control-Allow-Headers' response header.
  },

  success: function() {
    // Here's where you handle a successful response.
  },

  error: function() {
    // Here's where you handle an error response.
    // Note that if the error was due to a CORS issue,
    // this function will still fire, but there won't be any additional
    // information about the error.
  }
});
 
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