简体   繁体   English

Javascript Ajax发布数据

[英]Javascript Ajax Post Data

Javascript: 使用Javascript:

    function LoginButton1OnClick() {
        $.ajax({
            type: "POST",
            url: "Login/MainPageRegister",
            cache:false,
            data:
                {
                    LoginEmailText: $("#LoginEmailText").val(),
                    LoginPasswordText: $("#LoginPasswordText").val(),
                },
            success: function (mydata) {
                $("#BodyPage").html(mydata);
            }
        });
    }

ActionResult: 的ActionResult:

 [HttpPost]
    public ActionResult MainPageRegister(MyModel model)
    {
        return View();
    }

I post data to "MainPageRegister".However there occurs 2 problems. 我将数据发布到“MainPageRegister”。但是发生了2个问题。

  1. Url never changes in browser.After i post data. 网址永远不会更改。我发布数据后。

  2. I can not click " go back , go forward " in browser.If i refresh browser by enter "f5" , page opens previous page not current page. 我无法在浏览器中点击“ 返回前进 ”。如果我通过输入“f5”刷新浏览器,页面打开上一页而不是当前页面。

Any help will be appreciated. 任何帮助将不胜感激。

The problem is you are doing an ajax request, ajax request do not modify the browser history and therefore you cannot go back. 问题是你正在做一个ajax请求,ajax请求不会修改浏览器历史记录,因此你不能回去。

You can use HTML5 pushState() to make modifications to the history via JS, as well as hashtags. 您可以使用HTML5 pushState()通过JS和历史标签修改历史记录。 in JS you can use window.location.hash to modify the URL hashtag. 在JS中,您可以使用window.location.hash来修改URL主题标签。

The first answer of this question can bring more light to the subject: Change the URL in the browser without loading the new page using JavaScript 这个问题的第一个答案可以为主题带来更多亮点: 更改浏览器中的URL,而无需使用JavaScript加载新页面

  1. That is Ajax my friend. 那是我的朋友阿贾克斯。 Url won't change. 网址不会改变。 The data will arrive as Html because you are rendering a view. 数据将以Html形式到达,因为您正在渲染视图。 You can change chunks of the page but you are not redirecting the URL. 您可以更改页面的块,但不是重定向URL。

  2. That is Ajax again. 那是Ajax了。 Find the follow workaround Html 5 or JS 找到以下解决方法Html 5或JS

Quick Example to hash url 哈希网址的快速示例

var storedHash = window.location.hash; //get hash
    if (storedHash !== '') { //if no empty
        hashChanged(storedHash);
    }
window.setInterval(function () {
        if (window.location.hash != storedHash) { //chek if curent hash not == to stored hash when page was opened
            storedHash = window.location.hash;
            hashChanged(storedHash); //if change call a funcin
        }
    }, 100); //check hash if changed

function hashChanged(storedHash) {
    var link = storedHash.substr(1, storedHash.length); //get the link
    $.ajax({
        type: "POST",
        url: link+".php", //in browser url should look like #profile so when somebody 
        cache:false,
        data:
            {
                LoginEmailText: $("#LoginEmailText").val(),
                LoginPasswordText: $("#LoginPasswordText").val(),
            },
        success: function (mydata) {
            $("#BodyPage").html(mydata);
        }
    });
    }
  1. you come to website example.com 你来到网站example.com
  2. If you change to example.com#profile 如果您更改为example.com #profile
  3. It will automatically run ajax function to POST data to profile.php 它会自动运行ajax函数将POST数据发送到profile.php
  4. If the # will #page it will post data to page.php and etc play around I didnt check the code but it should work run console.log() for storedHash to check how is working! 如果#will #page它会将数据发布到page.php等等,我没有检查代码,但应该运行console.log()for storedHash来检查工作方式!

If you want to use ajax for posting data and you want to have support for url, you will need to design your app around this concept. 如果您想使用ajax发布数据并希望获得对url的支持,则需要围绕此概念设计应用程序。

I would recommend having a look at emberjs or sammyjs. 我建议看看emberjs或sammyjs。 These are great frameworks for building apps around this url-state-concept. 这些是围绕这个url-state-concept构建应用程序的优秀框架。

http://sammyjs.org/ http://emberjs.com/ http://sammyjs.org/ http://emberjs.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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