为什么Ajax将数据发布到localhost而不是提供的URL?

时间:2013-05-08 17:26:07

标签: javascript jquery ajax

所以我使用Ajax将表单发布到服务器,但是,不是将表单发送到URL,而是发送给自己。

这是代码

$("#psignup").click(function() {
    $.ajax({
           type: "POST",
           url: "example/default/mobile_user_register",
           data: $("#patientsignup").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

    return false; // avoid to execute the actual submit of the form.
});

在浏览器中,它似乎将发送到http://localhost:8080/?email=asdfa%40asd.com&password=asd&repeatpassword=&firstname=asd&username=&lastname=asd

我正在寻找的是“http://example.com/default/mobile_user_register?xxxxxxxxxxxxxx”。你能帮我理解为什么这不起作用吗?

4 个答案:

答案 0 :(得分:2)

使用JSONP

$.ajax({
    url: "http://example.com/default/mobile_user_register?xxxxxxxxxxxxxx",
    dataType: 'jsonp', //use jsonp data type in order to perform cross-domain ajax 
    crossDomain: true, 
    data: data, 
    success: callback,
    error: callback
});

答案 1 :(得分:0)

无法使用javascript AJAX发布到其他网址 - >它被大多数浏览器阻止。

答案 2 :(得分:0)

如果我理解正确并且托管此代码的域名是example.com,但您尝试调用example.net,那么AJAX安全性明确禁止此。

您需要做的是在您的服务器上设置一个微小的cgi代理,接收您想要进行的呼叫,然后获取该数据并将其带回给您。

假设您使用的是PHP,则您的通话将显示为myProxy.php?destination=http://example.net/whatever

然后代理将获取它在该目的地找到的内容并将其在响应中传回。

答案 3 :(得分:-5)

它并不神奇地知道example/default/mobile_user_register是您的域名。试试http://example.com/default/mobile_user_register