简体   繁体   中英

using MVC controller as webService

here is my project hirarchy:

在此输入图像描述

from browser.js I'm trying to call ManagerController:

 $("#jstree").jstree({
        "json_data": {

    "ajax": {   


                type: "GET",
                async: true,
                "url": "Controllers/Manager/Getlocations?userId='1234'",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                cache: false,
                success: function (msg) {                         
                        return msg;
                },
                error: function () {
                    // TODO process error
                }
            },

//continuation is less relevant

But I get the following error in chrome console:

GET http://localhost:1186/MainUgi/Controllers/Manager/Getlocations?userId= '1234'&_=1324071607446 404 (Not Found)

1) what should be the right path ?

2) what is the &_=1324071607446 which is concatinated to the end of my get request?

update

my controller looks like:

  public class ManagerController : Controller
    {

        public JsonResult GetLocations(string userId)
        {
            var locationsJson = 
            new {[  {"data": "[0]", .. some data...}  ]};

            return Json(locationsJson, JsonRequestBehavior.AllowGet);
        }

my request looks like:

Request URL:http://localhost:1186/MainUgi/Controllers/Manager/Getlocations?userId=1234
Request Method:GET
Status Code:404 Not Found
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:windows-1255,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:he-IL,he;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Type:application/json; charset=utf-8
Host:localhost:1186
Referer:http://localhost:1186/MainUgi/browser.htm
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7
X-Requested-With:XMLHttpRequest
Query String Parametersview URL encoded
userId:1234
Response Headersview source
Cache-Control:private
Connection:Close
Content-Length:2346
Content-Type:text/html; charset=utf-8
Date:Fri, 16 Dec 2011 22:00:37 GMT
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319

TIA

Hopefully this will help:

  1. Try removing the single quotes from userId='1234' --> userId=1234
  2. The _=1324071607446 parameter is jQuery appending a timestamp to the end of the request URL to prevent caching (notice how you've used cache: false ).

The basic MVC url mask is the following:

/{CONTROLLER}/{ACTION}?{...PARAMETERS}

By your example we have:

'/manager/getLocations?userId=1234'

And the controller should have the following code (example):

[ControllerAction]
public void getLocations( int userId ) {
    ViewData["UserId"] = userId;
    RenderView("GetLocations");
}

Now you need to have a view file to show the content. Create a folder (inside the root of the project), named "View", and inside it, create another one with the name of the controller (Manager), and create a view file called "GetLocations.aspx" (the one that we want asked to render).

To print the UserId variable in the view:

<%= ViewData["UserId"] %>

If it doesn't work, it's better for you to read a lot about the MVC Pattern. You can start here .

Try separating the url and the data into two separate commands:

url:  "Controllers/Manager/Getlocations",
data: { userId: '1234' }

Also try marking the method as [HttpGet] in case you have that method overloaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM