【问题标题】:Ajax Call using Jquery in HTML and take a response to other Html Page在 HTML 中使用 Jquery 进行 Ajax 调用并响应其他 Html 页面
【发布时间】:2017-05-18 05:00:03
【问题描述】:

我是第一次这样做。我想要在 HTML 页面中使用 AJAX 调用和 jQuery 的另一个 HTML 页面的数据。 下面是我的代码

这是我的 .js 代码

<script type="text/javascript">
$(document).ready(function()
{
    $('#submit').click(function(){
        var name =$('#name').val();
        var pass =$('#passsword').val();
        if(name=='' || pass==''){
            alert('first fill the details');
        }
        else {
            $.ajax({
                    method:'POST',
                    url:"login.html",
                    data:{name:name,
                    password:pass},
                    daatType:"HTML",
                    success:function(status) {
                            alert('success');
                            $('#div').html(status);
                        } 
                })

        }

    })
})

这是我的 HTML 代码

<form>
   <label>Name:</label><input type="text" id="name"><br>
   <label>Password:</label><input type="text" id="password"><br>
   <button type="submit" id="submit" >Submit</button>
</form>
<h2 id="div"></h2>

请帮忙

【问题讨论】:

  • 你的 jQuery 代码中可能有错字:daatType 而不是 dataType 我猜
  • status 变量的值是多少?您可以使用 url 参数将数据带到下一页。例如如果下一页是www.abc.com/def.html,那么你可以做www.abc.com/def.html#这是数据。在新的 html 页面中,您可以使用 javascript 获取此值: var x = location.hash;
  • 请明确您的数据是短数据还是长数据?如果它很短且不敏感,您可以像这样传递 URL。 window.location.href = site_url + 'status/' + 数据;在你的成功函数中做到这一点。

标签: javascript jquery html ajax


【解决方案1】:

dataType:"HTML" 更改为dataType:"json"

dataType:'json' // its mean server return type data

contentType:'json' // send data type to server should be json

【讨论】:

  • 我做了这个改变,但它没有解决问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 2014-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多