简体   繁体   English

AJAX POST 请求丢失

[英]AJAX POST requests getting lost

I'm having a problem trying to implement an AJAX request into a pre-existing website that I didn't create.我在尝试将 AJAX 请求实施到我没有创建的预先存在的网站时遇到问题。

I have no problems sending the data to PHP as a GET request, however POST requests and trying to access $_FILES is returning null.我将数据作为 GET 请求发送到 PHP 没有问题,但是 POST 请求并尝试访问 $_FILES 将返回 null。

Here is the AJAX:这是 AJAX:

var someData = 'test';

$.ajax({
    url: "post-data.php",
    method: "POST",
    dataType: "JSON",
    data: ({someData}),
    success: function(data) {
        console.log(data);
    }
});

PHP: PHP:

<?php echo json_encode($_POST['someData']); ?>

I believe the cause of the issue might lie within the htaccess file or be related to some other redirect that is in place on the site.我相信问题的原因可能在于 htaccess 文件或与网站上的其他重定向有关。 This website was built by a past staff member at the company, and I've had this same problem using AJAX POST with several other sites built by them.该网站是由该公司的一名前员工建立的,我在使用 AJAX POST 和他们建立的其他几个网站时遇到了同样的问题。

As changing from POST to GET works fine I don't think there is any problem with my very simple code.由于从 POST 更改为 GET 工作正常,我认为我非常简单的代码没有任何问题。

Is there some way I can test if the data is going missing due to a redirect, or could there be some other cause?有什么方法可以测试数据是否由于重定向而丢失,还是有其他原因?

First check the browser dev tools ctr+shift+J and see if there is a redirect.首先查看浏览器开发工具ctr+shift+J,看看有没有重定向。 If not then You need to set your datatype to json and depending on what version of JQUERY you are using you might have to use "type" instead of "method" if your JQUERY version < 1.9.0如果不是,那么您需要将数据类型设置为 json 并根据您使用的 JQUERY 的版本,如果您的 Z071EE600458087FD86908B5D4C9.937C 版本 < 1,您可能必须使用“类型”而不是“方法”。

    var someData = 'test';
    $.ajax({
         url: "post-data.php",
         method: "POST",
         dataType: "json",
         data: ({someData}),
         success: function(data) {
             console.log(data);
         }
      });

PHP code: PHP 代码:

     header("Content-Type: application/json", true);

If that doesnt work then make sure your URL is absolutely correct.如果这不起作用,请确保您的 URL 绝对正确。 No extra slashes or spaces.没有多余的斜线或空格。

I have managed to figure this out, there is a 302 redirect in place for all.php extetensions to a url without the extension.我已经设法弄清楚这一点,所有.php 扩展名都有一个 302 重定向到 url 没有扩展名。 There was also an error in my code.我的代码中也有错误。

Changing the URL in the AJAX to "post-data" without the.php extension allows me to see $_POST in PHP. Changing the URL in the AJAX to "post-data" without the.php extension allows me to see $_POST in PHP.

My other problem with not being able to access files came down to the way I was trying to send FormData with AJAX.我无法访问文件的另一个问题归结为我尝试使用 AJAX 发送 FormData 的方式。

My original code to do this was:我这样做的原始代码是:

var someData = 'test';
var fData = new FormData();

fData.append("images", $("input[name='fileUpload']").prop("files")[0]);

$.ajax({
    url: "post-data.php",
    method: "POST",
    dataType: "JSON",
    contentType: false,
    processData: false,
    data: ({someData, fData}),
    success: function(data) {
        console.log(data);
    }
});

The problem being that you can't send FormData and other variables at the same time.问题是您不能同时发送 FormData 和其他变量。 Instead I have to append my other data to the FormData:相反,我必须将我的其他数据 append 到 FormData:

var someData = 'test';
var fData = new FormData();

fData.append("images", $("input[name='fileUpload']").prop("files")[0]);
fData.append("someData", someData);

$.ajax({
    url: "post-data.php",
    method: "POST",
    dataType: "JSON",
    contentType: false,
    processData: false,
    data: fData,
    success: function(data) {
        console.log(data);
    }
});

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

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