2

I have such problem: my app consists of index.html and bunch of other html files for different pages of my app which I navigate by ng-route. I want to post some data to server, but I can do it only if I put on index.html

Here is my angular code:

    //post data to DB
    app.controller('sign_up', function ($scope, $http) {

    $scope.check_credentials = function () {

    document.getElementById("message").textContent = "";

    var request = $http({
        method: "post",
        url: window.location.href + "php/add_review.php",
        headers: { 'Content-Type': 'application/json' },
        data: {
            review: $scope.review
        }
    });

    request.success(function (data) {
        document.getElementById("message").textContent = "You have login successfully with email "+data+request;
    });
    }
    });

My PHP code:

    <?php

    $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    $review = $request->review;

    mysql_connect("localhost", "root", ""); 
    mysql_select_db("reherse");
    mysql_query("INSERT INTO reviews(review) VALUES('".$review."')");
    //echo $request
    echo $review; 
    ?>

And my HTML:

        <div id="login" ng-controller='sign_up'>
            <input type="text" size="40" ng-model="review"><br>
            <input type="password" size="40" ng-model="review"><br>
            <button ng-click="check_credentials()">Login</button><br>
            <span id="message"></span>
        </div>

When I add this HTML on index.html it passes successfully and gives me a message with data which is passed to server. When this html code is added on other html file it returns me all html page(gives me message like You have login successfully with email here goes html code...).

Will be really thankful for help!

1 Answer 1

1

Try using window.location.origin instead of window.location.href. The former will give you just the protocol, hostname, and port (e.g. http://localhost:8080) while the latter will give you the entire URL (e.g. http://localhost:8080/somepage.html)

That's why your HTTP request is failing on non-index pages, because the URL it's trying to load is incorrect; it's http://localhost:8080/somepage.html/php/add_review.php instead of http://localhost:8080/php/add_review.php. It works on the index likely because you're not specifying index.html and you're just loading http://localhost:8080, in which case window.location.origin would equal window.location.href (sort of, see note below) and give you the correct URL.

Note that window.location.origin does not contain a trailing slash, so make sure your HTTP request's URL string looks like this:

window.location.origin + "/php/add_review.php"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.