%5Bobject%20Object%5D(404未找​​到)试图通过AJAX提交的时候 [英] %5Bobject%20Object%5D (404 not found) when attempting to submit via AJAX

查看:664
本文介绍了%5Bobject%20Object%5D(404未找​​到)试图通过AJAX提交的时候的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过AJAX传递变量到PHP脚本,将运行一个MySQL查询。不过,我不断收到错误404未找​​到:HTTP:// myurl /数据库/%5Bobject%20Object%5D。

它看起来像它试图将数据发送到 HTTP:// myurl /数据库/%5Bobject%20Object%5D 我定义。真的,在这一个亏损...我想投入会工作的绝对URL。

下面是我的code。会永远感激任何帮助......

 <脚本类型=文/ JavaScript的>
功能insertData()
 {
    VAR数据ID = $('#数据ID)ATTR('值')。
    VAR产业= $('#行业)ATTR('值')。
   VAR地理位置= $('#地理位置)ATTR('值')。
    $。员额({
        键入:POST,
        网址:HTTP://myURL/database/InsertData.php
        数据:数据ID =+数据ID +&放大器;放大器;业界=+行业+&放大器;放大器;地理位置=+地理位置,
        });
返回false;
};

< / SCRIPT>
 

解决方案

正如安迪说, $。员额预期一个URL字符串作为第一个参数;如果你想传递一个选择对象,则必须使用 $就

当你试图调用 $职务。(小于选择对象>),选择对象被用作一个URL;在这一过程中,它被转换为字符串(和一个对象强制转换为字符串变成 [对象<类型>] 在Javascript;在这种情况下, [对象的对象] ),它得到的URL连接codeD(%5Bobject%20Object%5D ),并且,因为它不是以一个 / 或协议名称,它是PTED作为一个相对URL跨$ P $,并得到pfixed与目前的协议,域和路径$ P $。然后,由于没有更多的参数,一个空POST AJAX请求被发送到该URL。

另一个问题是,你使用&放大器;放大器; 来分隔参数;仅在HTML应该做的,在这里你应该只使用&安培; 。 (或数据对象,埃文说,这样你就不需要做考虑编码问题。)和 .VAL()。 ATTR('值')是不一样的;第一是字段的当前值,第二是它所具有的值加载页面时

的最简单的方法来正确地这是这样的:

 函数insertData(){
    。VAR数据= $('#数据ID,#工业,#地理定位)序列化();
    .post的$('HTTP://myURL/database/InsertData.php,数据);
    返回false;
}
 

这假定三个字段具有相同的名称 ID $.serialize 使用名称的参数名。

I am trying to pass variables via ajax to a PHP script that will run a MySQL query. However, I keep getting the error 404 Not Found with: "http://myurl/database/%5Bobject%20Object%5D".

It looks like it's trying to send the data to http://myurl/database/%5Bobject%20Object%5D instead of the PHP script I've defined. Really at a loss on this one... I thought putting the absolute URL in would work.

Below is my code. Would be eternally grateful for any help....

<script type="text/javascript">
function insertData()
 {
    var dataid= $('#dataid').attr('value');
    var industry = $('#industry').attr('value');
   var geolocation = $('#geolocation').attr('value');
    $.post({
        type: "POST",
        url: "http://myURL/database/InsertData.php",
        data: "dataid="+ dataid+"&amp;industry="+ industry +"&amp;geolocation="+ geolocation,
        });
return false;
};

</script>

解决方案

As andi said, $.post expects an URL string as a first parameter; if you want to pass an option object, you must use $.ajax.

When you try to call $.post(<option object>), the option object is used as an URL; in that process, it is cast to a string (and an object cast to a string becomes [object <type>] in Javascript; in this case, [object Object]), it gets URL-encoded (%5Bobject%20Object%5D), and, since it does not start with a / or a protocol name, it is interpreted as a relative URL and gets prefixed with the current protocol, domain and path. Then, since there are no more parameters, an empty POST AJAX request is sent to that URL.

Another problem is that you use &amp; to separate the parameters; that should be done only in HTML, here you should just use &. (Or a data object, as Evan said, that way you don't need do think about encoding issues.) And .val() and .attr('value') are not the same; the first is the current value of the field, the second is whatever value it had when the page was loaded.

The simplest way to this correctly is this:

function insertData() {
    var data = $('#dataid,#industry,#geolocation').serialize();
    $.post('http://myURL/database/InsertData.php', data);
    return false;
}

This assumes that the three fields have the same name and id as $.serialize uses name for the parameter name.

这篇关于%5Bobject%20Object%5D(404未找​​到)试图通过AJAX提交的时候的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆