V
V
Vladislav2014-09-08 14:56:16
PHP
Vladislav, 2014-09-08 14:56:16

How to add Ajax to MVC?

How to send a POST request with Ajax if the URL is like this:
//mysite.ru/authorization/name/pupkin/pass/123
Is there a way to send such data?

And if not, how to make it understand such a URL: //mysite.ru/autorization?name=pupkin&pass=123

.htaccess is as follows:

RewriteEngine on
RewriteBase /
AddDefaultCharset utf-8
 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey, 2014-09-08
@DubecZ

How to send a POST request with Ajax if the URL is like this:
//mysite.ru/authorization/name/pupkin/pass/123

And why excuse you then POST? Figachte GET. Do you understand at all that you are thus laying out the password for all to see? No SSL will save.
And so, take any library for routing or parse the request on the server. But IMHO you are doing a great stupidity by sending data in this way. Yes, you can shove data into the query string during a post request and parse the urls, but what's the point of POST then?
It is sad...

V
Viktor Vsk, 2014-09-08
@viktorvsk

Apparently, you have problems with authorization. That's why people invented sessions. Log in once, for example, with Ajax like this:

$.post('http://example.com/auth', data: { pass: "qwerty", pass_confirmation: "qwerty" })
, something like this. The server creates a session for you, returns a cookie with this session. You keep it and continue to climb the pages, without the need for authorization on each.
Maybe it was a psychic mod, but if the problem is something else, then I don’t understand what the “work of ajax and mvc” is.
As I understand it, the problem here is not with routing.
And in general, try to solve typical tasks with ready-made tools (frameworks). Save lots of time

A
Alexander Tartmin, 2014-09-08
@baskerville42

Let's take as an example the ExpressJS framework running on the NodeJS platform
, you can parse the URL in this way

app.get('/fruit/:fruitName/:fruitColor', function(req, res) {
    var data = {
        "fruit": {
            "apple": req.params.fruitName,
            "color": req.params.fruitColor
        }
    }; 

    send.json(data);
});

This is like an example of sending data to the server.
If the parameters need to be parsed on the client, then the same AngularJS has the ability to parse parameters from this type of URL
.when('/pages/:pageName', {
            templateUrl: 'views/single.html',
            controller: 'singlePageController'
        })

A
Alexey, 2014-09-08
@HaJIuBauKa

And why not (without going into the question of why you need such password manipulations, although this is interesting)?
First, what MVC are you using?
Normal MVC on the side, how many routes are there in the URL, it will parse and lay out everything for you as an array. And the parameters will come to the data post.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question