简体   繁体   English

AJAX呼叫未发送成功响应

[英]AJAX Call Not Sending Success Response

//MAKE AJAX CALL TO REPOPULATE TABLE
        var newData = 'page_number=1&type=2';

        $.ajax({
            type: 'POST', // HTTP method POST or GET
            url: 'http://www.myurl.net/form_validate.php', //Where to make Ajax calls
            dataType:'text', // Data type, HTML, json etc.
            data:newData, //post variables
            success:function(response){

                //REFORMAT UPON SUCCESSFUL AJAX CALL
                alert(response);

            },
            error:function (xhr, ajaxOptions, thrownError){
                alert(xhr + " " + ajaxOptions + " " + thrownError); //throw any errors
            }
        });

All I've put in my PHP file is: 我放入PHP文件中的所有内容是:

<?php echo "test"; ?>

When I go straight to that file, it echoes 'test.' 当我直接转到该文件时,它会回显“测试”。 When I try to run the AJAX function on the click of a button it gives me the error: 当我尝试单击按钮来运行AJAX函数时,出现以下错误:

[object Object] error 

in an alert window. 在警报窗口中。 I've put the absolute URL to the file because I was thinking that the relative linking I was using was wrong but it now seems like it's some other issue. 我将绝对URL放入文件中是因为我认为我正在使用的相对链接是错误的,但是现在看来这是另一个问题。 Am I overlooking a simple syntax error? 我是否忽略了一个简单的语法错误? Sorry if this is super basic but I can't seem to figure this out after working on it for a really long time. 抱歉,如果这是超级基础,但是经过很长时间的研究之后,我似乎无法弄清楚。 Thanks for your help. 谢谢你的帮助。

The problem is your absolute url . 问题是您的绝对网址。 Some browsers have problems dealing with absolute urls, consider it as a cross-domain request and block it even if it's not a cross-domain request . 某些浏览器在处理绝对URL时遇到问题,将其视为跨域请求, 即使不是跨域请求也将其阻止。 You should try with relative url instead 您应该尝试使用相对网址

The problem might be in the url , try with relative path instead of absolute. 问题可能出在url ,请尝试使用相对路径而不是绝对路径。 You named the file was in the same folder as the .js file, so try 您命名的文件与.js文件位于同一文件夹中,因此请尝试

url: '/directory_path_to_the_same_folder_as_the_JS_file/form_validate.php',

Try using done with jQuery.post instead . 尝试使用jQuery.post代替 done

For example: 例如:

jQuery.post('form_validate.php', data).done(
        function(data, textStatus, jqXHR) {
                            alert(jqXHR.responseText);

        }).fail(function(jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);

}).complete(function() {
    // Common code when request completes
});
  • If your error is [object Object] error NOT FOUND then the reason is the specified url in AJAX is not correct. 如果您的错误是[object Object] error NOT FOUND找不到[object Object] error NOT FOUND则原因是AJAX中指定的url不正确。

  • If your error is [object Object] error INTERNAL SERVER ERROR , its because of the error in your server file ie the php file ,error like variables not defined correctly etc.. 如果您的错误是[object Object] error INTERNAL SERVER ERROR ,则是由于服务器文件即php文件中的错误,错误(如未正确定义的变量等)引起的。

  • Some times error may occur due to cross domain, if you have not specified headers when in case your php file is not in the same domain 有时由于跨域可能会发生错误,如果您未指定标头,以防您的php文件不在同一域中

Hope this helps 希望这可以帮助

Thank you 谢谢

function abc(){
  var ret=true;
 $.ajax({
           type: 'POST',
           url: 'send_password.php',
           data: 'mail_to=mymail&new_password=pwd',
           async:false,
           success: function(response){

           },
           error: function(r){

               ret=false;
           }
       });
return ret;
}

    alert(abc());

Have a look at this testfiddle 看看这个提琴

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

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