ajax jQuery asp.net error Unexpected token < - asp.net

I am having trouble adding ajax functionality to existing asp.net 4 website.
I have both tried creating webmethod in the aspx page and also tried asmx, but in both cases I get this error Unexpected token <
this is my jQuery:
function postAssets(datapm) {
$.ajax({
type: "POST",
timeout: 20000,
tryCount: 0,
retryLimit: 10,
url: "talk.asmx/HelloWorld",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
console.log('success postAssets '+msg.d);
},
complete: function (jqXHR, status) {
if (status == 'success' || status == 'notmodified') {
console.log('complete postAssets' + jqXHR.responseText);
}
},
error: function (req, status, error) {
console.log('error postAssets');
}
});
}
and this is what is in asmx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for talk
/// </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 talk : System.Web.Services.WebService {
public talk () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
}
I wonder if I am missing any webconfig items, or is it all built in in asp.net 4?
<configuration>
<connectionStrings />
<system.web>
<compilation debug="true" targetFramework="4.0" />
<machineKey validationKey="BA5B68AB87AAEA30753960733E796568" decryptionKey="FAF15E4015737A7695D9761" validation="SHA1" />
<authentication mode="Windows" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>

Are you returning JSON or markup? Your call to jQuery's ajax() method is expecting JSON but if you're returning markup that starts with a < character then I could imagine it throwing that exception.

I guess the problem is that you declare your Ajax type as POST while in your ASP Controller you declare HelloWorld() as WebMethods. That's why your ajax can't find your HelloWorld Function.
Try to delete this line:
[WebMethods]
and see if that works.

Related

How to create a web service in Sitecore

There is a tabbed panel like HTML in the page, where each tab click will show different data.
Trying to accomplish this with Ajax calls.
In the Visual studio project, I created a web service test.asmx, inside Services folder. So on publish, it is saved at wwwroot/MyApp/Website/Services/test.asmx
asmx.cs
[System.Web.Script.Services.ScriptService]
public class test : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
ascx
<asp:ScriptManager ID="_scriptManager" runat="server">
<Services>
<asp:ServiceReference Path="/Services/test.asmx" />
</Services>
</asp:ScriptManager>
<script type="text/javascript">
jQuery(function ($) {
console.log("howdy"); //this does print
var Param1 = 'one';
var Param2 = 'two';
$.ajax({
type: "GET",
url: "/Services/test.asmx/HelloWorld",
data:"",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: "true",
cache: "false",
success: function (msg) {
alert(msg.d);
},
Error: function (x, e) {
alert("errrrr");
}
});
});
</script>
On browsing the page, there is this error in the browser console.
GET http://MyApp/Services/test.asmx/HelloWorld 500 (Internal Server
Error) jQuery.noconflict.js:16
Is this the right way to make the call
Is it because of the prototype.js conflict (not again !!). I already said jQuery(function ($) {..., but still.....
Personally I wouldn't use a web service because I dont think you are going to have the right Sitecore context for doing Sitecore work. I would just use MVC route and use it like an web api.
The controller
using System.Web.Mvc;
using Bonfire.Kickfire.Analytics.Dto;
using Bonfire.Kickfire.Analytics.Models;
namespace Bonfire.Kickfire.Analytics.Controllers
{
public class VisitorController : Controller
{
[HttpGet]
public JsonResult VisitorDetailsJson()
{
var item = Sitecore.Context.Item;
var vi = new VisitorInformation();
var trackerDto2 = vi.GetTrackerDto();
return Json(trackerDto2, JsonRequestBehavior.AllowGet);
}
}
}
The route
using System.Web.Mvc;
using System.Web.Routing;
using Sitecore.Pipelines;
namespace Bonfire.Kickfire.Analytics.Pipelines.Initialize
{
public class InitRoutes : Sitecore.Mvc.Pipelines.Loader.InitializeRoutes
{
public override void Process(PipelineArgs args)
{
RouteTable.Routes.MapRoute(
"Profile", // Route name
"VisitorData", // URL with parameters
new {controller = "Visitor", action = "VisitorDetailsJSON"},
new[] {"Bonfire.Kickfire.Analytics.Controllers"});
}
}
}
The patch file
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<initialize>
<processor type="Bonfire.Kickfire.Analytics.Pipelines.Initialize.InitRoutes, Bonfire.Kickfire.Analytics"
patch:before="processor[#type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']"/>
</initialize>
</pipelines>
</sitecore>
</configuration>

Create New Asmx Page in asp.net

I am creating an asmx web service.
In visual studio 2012 on the project name I right clicked and added an web service name UserDetails.asmx
Now I am trying to use the web service in the js file. like this
$.ajax({
type: "POST",
url: "UserDetails.asmx/HelloWorld",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('success');
},
error: function () {
alert("Error");
}
});
Its showing error that POST http: //192.168.9.185/GPS/UserDetails.asmx/HelloWorld 500 (Internal Server Error)
Is there any fault in my code or I am missing something so that its showing error.
My Asmx Page
<%# WebService Language="C#" CodeBehind="~/App_Code/UserDetails.cs" Class="UserDetails" %>
My Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for UserDetails
/// </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 UserDetails : System.Web.Services.WebService {
public UserDetails () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
}
Edit
Short Answer: just uncomment [System.Web.Script.Services.ScriptService]
Digging deep in the error message by using
error: function (xhr,status, error) {
alert("Error");
}
and inspecting xhr revealed that the actual error was something else (Only Web services with a [ScriptService] attribute on the class definition can be called from script).
I used [WebMethod] in my aspx code behind and whenever I received such message, I found out that there was an error in the data I am passing. So try adding data:"{}", in your $.ajax()
as described in this post
When making a read-only request, the empty data parameter is the key. For reasons unknown, jQuery does not properly set the specified content-type when there is no data included.

WCF Web Service inside ASP.NET Application - how to support nullable parameters?

I'm trying to allow flexible method parameters inside of my WCF service, but I've run into a few issues.
First, I'll just explain the goal. I want to be able to declare a web method, such as:
[OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
public StatisticEventArgs GetStatistic(DateTime? startDate = null, DateTime? endDate = null)
{
return new StatisticEventArgs() { Count = 50; }
}
and be able to call it through the jquery $.ajax method:
$.ajax({
type: 'GET',
url: 'service/MyService.svc,
data: '', // or '?startDate=12/1/2011&endDate=12/10/2011','?startDate=12/1/2011', etc. Any combination of startDate or endDate parameters
contentType: "application/json",
dataType: "json",
processdata: true,
success: endCallback,
error: errorCallback
});
The goal is to create methods where all parameters are optional, and for whichever parameters are not specified, their default value will be used instead. In this example, I want to be able to call the method where I can provide either the startDate
or endDate parameter, both, or neither.
I quickly found that the default QueryStringParameter class (as seen in this question: In the WCF web programming model, how can one write an operation contract with an array of query string parameters (i.e. with the same name)?) doesn't support nullable types, but JsonQueryStringConverter does. Also, as noted in the above question, there is a bug in .NET 4.0 and below related to this solution that prevents the JsonQueryStringConverter class from being instantiated in a custom behavior (GetQueryStringConverter is never called).
After more research, I found this Microsoft bug that provides a workaround, but only if instantiating the service through code.
My question is, how can I apply that workaround in an ASP.NET WCF Web service? I believe I need to set it up in the web.config, but I'm not sure how to accomplish that. Here is the relevant example code for what I have so far (I've modified names, so it may contain some typos):
WCF class contained inside of my ASP.NET Web Application project:
namespace Namespace.Services
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
[OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
public StatisticEventArgs GetStatistic(DateTime? startDate = null, DateTime? endDate = null)
{
return new StatisticEventArgs() { Count = 50; }
}
public class NullableTypeBehaviorBehaviorExtension : BehaviorExtensionElement
{
public override Type BehaviorType
{
get { return typeof(NullableTypeBehavior); }
}
protected override object CreateBehavior()
{
return new NullableTypeBehavior();
}
}
public class NullableTypeBehavior : WebHttpBehavior
{
protected override System.ServiceModel.Dispatcher.QueryStringConverter GetQueryStringConverter(OperationDescription operationDescription)
{
return new JsonQueryStringConverter();
}
}
}
Javascript:
$.ajax({
type: 'GET',
url: 'service/MyService.svc/GetStatistic',
data: '', // or '?startDate=12/1/2011&endDate=12/10/2011','?startDate=12/1/2011', etc. Any combination of startDate or endDate parameters
contentType: "application/json",
dataType: "json",
processdata: true,
success: endCallback,
error: errorCallback
});
web.config:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="nullableTypeBehavior" type="Namespace.Services.NullableTypeBehaviorBehaviorExtension, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"></serviceHostingEnvironment>
<services>
<service name="MyWcfService">
<endpoint address="" behaviorConfiguration="MyBehaviorConfig"
binding="webHttpBinding" contract="Namespace.Services.MyService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="web">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="MyBehaviorConfig">
<nullableTypeBehavior />
</behavior>
</endpointBehaviors>
</behaviors>
</client>
UPDATE 1: SOLUTION
Here's the answer I ended up with (Courtesy of VinayC):
public class CustomFactory : System.ServiceModel.Activation.ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type t, Uri[] baseAddresses)
{
return new CustomHost(t, baseAddresses);
}
}
public class CustomHost : ServiceHost
{
public CustomHost(Type t, params Uri[] baseAddresses) : base(t, baseAddresses) { }
protected override void OnOpening()
{
var nullableTypeBehavior = new NullableTypeBehavior();
foreach(var ep in this.Description.Endpoints)
{
ep.Behaviors.Add(nullableTypeBehavior);
}
base.OnOpening();
}
}
and at the top of my MyService.svc file:
<%# ServiceHost Language="C#" Debug="true" Service="Namespace.Services.MyService" CodeBehind="MyService.svc.cs" Factory="Namespace.Services.CustomFactory" %>
UPDATE 2: Issue with passing dates for this solution
Currently passing dates does not work:
http://localhost/Services/MyService.svc?startDate=11/10/2011&endDate=11/11/2011
but these requests work:
http://localhost/Services/MyService.svc?startDate=null&endDate=null
http://localhost/Services/MyService.svc
Seems like that's JsonQueryStringConverter specific, so I'm going to look into it and post back when I find a solution. Seems like it should be simple enough to debug.
UPDATE 3: Solution for passing dates:
The problem is that the dates I was passing are not in the expected format for .NET JSON. I was passing something like:
"12/01/2011"
and .NET JSON (including JsonQueryStringConverter) expects this format:
"\/Date(1283219926108)\/"
That date format is not native to javascript. In order to retrieve a date in this format, I found this post. After adding this script, I can do this in jQuery, and the date will be parsed appropriately:
$.ajax({
type: 'GET',
url: 'service/MyService.svc/GetStatistic',
data: '?startDate=' + JSON.stringifyWCF(new Date('12/1/2011'))
contentType: "application/json",
dataType: "json",
processdata: true,
success: endCallback,
error: errorCallback
});
Everything seems to work as expected. I can ignore any optional parameters, set them to null, or give them a value and all requests work.
Updated the Microsoft bug with the workaround. The workaround is a bit more elegant than the solution provided here.
I am not certain if you need nullable types. If you want to pass multiple date/time values - why not use signature such as
public StatisticEventArgs GetStatistic(DateTime[] startDates = null, DateTime[] endDates = null)
In your CustomQueryStringConverter, you need to check if the parameter is null/empty string then your array will be null. Start dates and end dates will be correlated by index. Only possible combination that you may not able to pass would be having one pair where start-date is missing and another pair where end date is missing. As a work-around, you can always use some epoch start date.
EDIT:
If you are trying to apply the work-around from MS Connect in your WCF service then you need to create custom service host factory. For example,
public class CustomFactory : ServiceHostFactory
{
public override ServiceHost CreateServiceHost( Type t, Uri[] baseAddresses )
{
return new CustomHost( t, baseAddresses )
}
}
public class CustomHost : ServiceHost
{
public DerivedHost( Type t, params Uri baseAddresses ) : base( t, baseAddresses ) {}
public override void OnOpening()
{
var nullableTypeBehavior = new NullableTypeBehavior();
foreach(var ep in this.Description.EndPoints)
{
ep.Behaviors.Add(nullableTypeBehavior);
}
}
}
And finally use #ServiceHost directive in svc file to plug-in the factory - for example:
<% #ServiceHost Factory=”CustomFactory” Service=”MyService” %>

ASP.NET Web Service to call from JS

I am constantly finding a good guide about how to write a web service using .NET with Visual Studio 2010 so I can utilize it with my HTML based website using AJAX.
I know there was a way called the ASMX way but now it's more updated to ServiceHost so what I need is a simple guide which can drive me through how to create asp.net web service using ServiceHost object.
Sorry if it sounds ambiguous or childish.
Place the ScriptManager control on your page and add a reference to your .asmx service:
<asp:ScriptManager ID="myManager" runat="server">
<Services>
<asp:ServiceReference Path="~/MyService.asmx" />
</Services>
</asp:ScriptManager>
In the code-behind of your web-service declare you web method (notice the ScriptService attribute):
namespace MyServices
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class MyService : System.Web.Services.WebService
{
[WebMethod]
public string SayHello(name)
{
return "Hello, " + name;
}
}
}
Now you can consume the web-service from the Javascript like the following:
function queryWebService() {
MyServices.MyService.SayHello('Bobby',
function(result, userContext) {
alert('Web-service response: ' + result);
}, function(result) {
alert('Error!');
});
}
UPDATE
If you want to consume the web-service by simply sending an HTTP GET requests then you can do the following:
Decorate your web-method with a ScriptMethod attribute:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string SayHello(name)
{
return "Hello, " + name;
}
Notice the UseHttpGet property which is set to True. You probably also need to modify the web.config file to allow this kind of interaction:
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
Now you can make a simple HTTP GET request to your web-service like shown below (the example uses jQuery.ajax):
$.ajax({
url: "/MyService.asmx/SayHello?name=Bobby",
success: function(transport) {
alert('Web-service response: ' + transport.responseText);
}
});
Hope this will help you.

ASP.NET jQuery error: Unknown Web Method

This is my first time attempting to call an ASP.NET page method from jQuery. I am getting a status 500 error with the responseText message that the web method cannot be found. Here is my jQuery $.ajax call:
function callCancelPlan(activePlanId, ntLogin) {
var paramList = '{"activePlanId":"' + activePlanId + '","ntLogin":"' + ntLogin + '"}';
$.ajax({
type: "POST",
url: "ArpWorkItem.aspx/CancelPlan",
data: paramList,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function() {
alert("success");
},
error: function(xml,textStatus,errorThrown) {
alert(xml.status + "||" + xml.responseText);
}
});
}
And here is the page method I am trying to call:
[WebMethod()]
private static void CancelPlan(int activePlanId, string ntLogin)
{
StrategyRetrievalPresenter presenter = new StrategyRetrievalPresenter();
presenter.CancelExistingPlan(offer, ntLogin);
}
I have tried this by decorating the Web Method with and without the parens'()'. Anyone have an idea?
Your web method needs to be public and static.
Clean the solution and rebuild. I've seen webmethods throw 500's until you do this.
Add public static before your method...
ex.
[WebMethod]
public static string MethodName() {}
For ajax success:
For me, it was helpful to make:
App_Start\RouteConfig
set
from
settings.AutoRedirectMode = RedirectMode.Permanent;
to
settings.AutoRedirectMode = RedirectMode.Off;
make your using method:
public
static
add:
using System.Web.Services;
and on top of method using just:
[WebMethod]
is enough
First Of All Don't Forget To Include
using System.Web.Services;
And Make Sure Your Method Should Be Public And Static and avoid adding Multiple Scripts in same Page like jquerymin.js shouldn't be used for every Function/Method in same Page
[WebMethod]
public static sting MethodName(){}
I Had The Same Issue Which Using Ajax And Jquery To Check Username Exists
Or Not.

Resources