简体   繁体   English

为什么这个ajax请求失败了?

[英]Why is this ajax request failing?

This is probably a simple problem but I don't understand why this is happening. 这可能是一个简单的问题,但我不明白为什么会这样。 Here is the reduced code: 这是简化的代码:

Ajax call : Ajax调用:

mydata = {'action':'update','options':options};
        console.log(mydata);
        $.ajax({
            url: 'dt/scripts/stoplight.php',
            data: mydata
            }).success(function(data){
            if (data == 1) {
                alert("Options Updated");
            }else{
                alert(data);
            }
        })

My data looks like this: 我的数据如下:

action
    "update"
options
    Object { OMS-S="0", OMS-N="0", OHS="0"}

To clarify, this is a copy and paste from the browser console. 为了澄清,这是来自浏览器控制台的复制和粘贴。 The object is a valid object and is being passed via get like so: 该对象是一个有效的对象,并通过get传递:

https://*pathtomysite*/dt/scripts/stoplight.php?action=update&options%5BOMS-S%5D=0&options%5BOMS-N%5D=0&options%5BOHS%5D=0

This request hangs indefinately. 这个请求无限期地挂起。

https://*pathtomysite*/dt/scripts/stoplight.php?action=update&options%5BOMS-S%5D=0&options%5BOMS-N%5D=0&options%5BOHS%5D=1

To further clarify. 进一步澄清。 Options is being generated like this: 选项生成如下:

$("#stoplight_apply").click(function(){
                var radios = $("#stoplight_options").find("input:radio:checked");
                options = {};
                $.each(radios, function( key, value) {
                    options[value.name] = value.value;
                });
                set_stoplight_options(options);
            })

This one works fine. 这个工作正常。

If any of these options are set to anything other than 0 then the php script it is going to works great! 如果将这些选项中的任何一个设置为0以外的任何选项,则php脚本将会运行得很好! If all of them are 0 then it hangs and loads indefinitely. 如果所有这些都是0,那么它会无限期地挂起并加载。

I commented out all the PHP that could be causing problems so currently the script does this: 我注释掉了可能导致问题的所有PHP,所以目前脚本执行此操作:

$action = $_GET['action']; //Get or update
print_r($_GET['action']);
print_r($_GET['options']);

Why is this happening? 为什么会这样?

UPDATE: 更新:

I think I found the problem. 我想我发现了这个问题。 All I did was change the word 'options' to 'test' and the php to print_r($_GET['test']) and it works fine. 我所做的就是将'options'改为'test',将php更改为print_r($ _ GET ['test']),它运行正常。 WTH? WTH?

Try 尝试

Use $_POST not $_GET 使用$_POST而不是$_GET

$action = $_POST['action'];
print_r($_POST['action']);
print_r($_POST['options']);

Ajax : Ajax:

var mydata = {'action':'update','options':options};
$.ajax({
   type: 'POST',
   url: 'dt/scripts/stoplight.php',
   data: mydata,
   success: function(data){
       if(data.length) {
         alert('done')
       }            
   }
});
  • Please do one by one (PHP -> JS -> HTML) 请逐一进行(PHP - > JS - > HTML)
  • Ajax : post some data and get data like console.log(data); Ajax:发布一些数据并获取console.log(data);console.log(data); and see what you got. 看看你得到了什么。
  • Then do statement : if(data.length) if(data.sucess==='YeahDone') (JSON) whatever.. (check by your PHP script if all success echo 'YeahDone'; for example.. 然后做声明: if(data.length) if(data.sucess==='YeahDone') (JSON)无论如何..(通过PHP脚本检查是否所有成功echo 'YeahDone';例如..
  • So do something with HTML or alert or whatever 所以做一些HTML或警报或其他什么

Try 尝试

 options

  Object { "OMS-S":"0", "OMS-N":"0", "OHS":"0"}

Now I see why quotes are a good practice :P 现在我明白为什么引用是一个好习惯:P

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

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