weixin_33686714 2012-02-07 01:11 采纳率: 0%
浏览 18

jQuery Ajax似乎不起作用

Here is my HTML. Just a simple form:

<form>
    Username:
    <input type="text" id="username"/>
    <br/>
    Password:
    <input type="password" id="password"/>
    <br/>
    <input type="submit" id="submit" value="submit"/>
</form>

Here is my JS associated with it:

function init(){
$("#submit").click(function() {   
    var url = "http:example.com/mail";
    alert("what?");
    $.ajax(url, {
        type : 'post',
        data : {
            'username' : $("#username").val(),
            'password' : $("#password").val()
        },
        success : function() {
            alert("done");
        }
    });
});
}

After I clicked on the submit button, the $.ajax function is supposed to do a post to the URL where I keeps my server running.

However, from my server side log or firebug network monitoring, I didn't see any sign of the POST method. (The first alert was triggered but the second wasn't.)

They are two different applications that I developed, so after I did some research, here is one explanation:

Since $.ajax() uses XMLHttpRequest, it is subject to XHR's cross-domain restriction. Are your SiteA and SiteB on different hosts/ports? If so, you're seeing the expected behavior.

Is that so? If so, is there any workaround?

  • 写回答

3条回答 默认 最新

  • 撒拉嘿哟木头 2012-02-07 01:12
    关注

    You need return false; at the end of the click handler to prevent the default submission of the form. Although once you prevent the form from submitting, you will still have the cross-domain restriction, which is not trivial to solve. Look into jsonp for a possible solution.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题