PDA

View Full Version : Ajax URL Problem



mjcee
7 Aug 2017, 12:10 PM
I am having a problem with the sample 'Ext JS Application and Web API' application generated by the latest Visual Studio Plugin, ExtJS 6.5.1 and cmd 6.5.1.240. When I make an Ajax call to an API Controller, the program is looking for the the controller in the localhost:nnnnn/Sencha/Api directory. I expected it to look in the localhost:nnnnn/Api directory.

Here is the code I have inserted into the launch function of Application.js:



Ext.define('ExtApplication.Application', {
extend: 'Ext.app.Application',


name: 'ExtApplication',


quickTips: false,
platformConfig: {
desktop: {
quickTips: true
}
},


launch: function () {
Ext.Ajax.request({
url: 'Api/AccountManager/GetAccountManagerInfo',
scope: this,
callback: this.onAfterAjaxReq
});
},


onAfterAjaxReq: function (options, success, result) {
},


onAppUpdate: function () {
Ext.Msg.confirm('Application Update', 'This application has an update, reload?',
function (choice) {
if (choice === 'yes') {
window.location.reload();
}
}
);
}
});


Attached is the result of running the application showing that it is expecting to find the controller in the Sencha/Api directory rather than the Api directory. What am I doing wrong?

mjcee
8 Aug 2017, 9:00 AM
I think the problem is not with the application generated by the Visual Studio Plugin but with the way that I am using the application. Below is my api controller program that I saved in AccountManagerController.cs in the Api directory.



using System.Web.Http;


namespace ExtApplication.Controllers
{
public class AccountManagerController : ApiController
{
public IHttpActionResult GetAccountManagerInfo()
{
return Ok(new { data = "ABC", success = true });
}
}
}




As shown in the previous entry, I am attempting to call this controller with the following ajax call:



Ext.Ajax.request({
url: 'Api/AccountManager/GetAccountManagerInfo',
scope: this,
callback: this.onAfterAjaxReq
});
},




What should the ajax call and the corresponding controller program look like to use the application structure of the generated application?

mjcee
10 Aug 2017, 7:57 AM
I submitted a ticket and got this response that fixed the problem:

The simplest way for you is to change default controller routing. You can do it in WebApiConfig.cs file:


​using System;using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace MyApp
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services

// Web API routes
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "sencha/api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
It's needed to change routeTemplate parameter to "sencha/api/{controller}/{id}" and then your example should work.