Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax调用未获得成功响应_Javascript_Php_Jquery_Ajax_Response - Fatal编程技术网

Javascript Ajax调用未获得成功响应

Javascript Ajax调用未获得成功响应,javascript,php,jquery,ajax,response,Javascript,Php,Jquery,Ajax,Response,我们有两个文本字段。我们试图通过ajax调用发布用户名和密码 我们已经使用了PHP服务,用户名和密码确实成功地保存到了数据库中 但我们没有从成功或失败中得到任何回应 守则: PHP: Ajax调用:- $.ajax({ url:'http://192.168.3.134:8080/ekova/postevent.php', type:'POST', data:{ecovauserName :usernam

我们有两个文本字段。我们试图通过ajax调用发布用户名和密码

我们已经使用了PHP服务,用户名和密码确实成功地保存到了数据库中

但我们没有从成功或失败中得到任何回应

守则:

PHP:

Ajax调用:-

 $.ajax({
               url:'http://192.168.3.134:8080/ekova/postevent.php',
               type:'POST',
               data:{ecovauserName :username,ecovauserage:password},
               contentType: "application/json; charset=utf-8",
                dataType: "jsonp",
               success:function(responsive){
                alert(responsive);
               },
               error:function(w,t,f){
                 console.log(w+' '+t+' '+f);
               }
            });
上面的代码运行良好。用户名和密码已成功存储在数据库中。但我们需要获得成功的失败响应

我的成功:函数未被调用,因此我的警报框从未运行以通知我成功

请告诉我代码中的错误。

数据类型应为“json”而不是“jsonp”


我建议您使用“text”而不是“json”,因为您返回的是正常的文本结果,而不是json编码的数据。

您可以尝试删除行数据类型:“jsonp”-这取得了一些成功

见此帖:

编辑-尝试将basic console.log语句同时放入success和fail中,看看是否命中了其中一个

$.ajax({
           url:'http://192.168.3.134:8080/ekova/postevent.php',
           type:'POST',
           data:{ecovauserName :username,ecovauserage:password},
           contentType: "application/json; charset=utf-8",
            dataType: "jsonp",
           success:function(){
            console.log('in success');
           },
           error:function(){
             console.log('in error');
           }
        });
检查是否出现错误:

error: function(xhr, status, error) {
  var error1 = eval("(" + xhr.responseText + ")");
  console.log(error1.Message);
  console.log(geturl.getAllResponseHeaders());
  alert("error!"+ geturl.getAllResponseHeaders());
}

在php调用中返回
json\u encode
响应,如下所示:

echo json_encode(array('status' => 'in success'));

您为什么不使用呼叫返回的状态代码,例如:

$.ajax({
  url:'http://192.168.3.134:8080/ekova/postevent.php',
  type:'POST',
  data:{ecovauserName :username,ecovauserage:password},
  contentType: "application/json; charset=utf-8",
  dataType: "jsonp",
  statusCode: {
    404: function () {
      //error
    },
    200: function (data) {
      //response data
    }
  }
});

您似乎正在使用跨域查询,jsonp使之成为可能

因此,在php代码中,必须返回jsonp格式:

$response = array('success' => false, 'message' => '');

if (isset($_GET['ecovauserName']) && isset($_GET['ecovauserage'])) {
    $ecovauserName = $_GET['ecovauserName'];
    $ecovauserage = $_GET['ecovauserage'];

    $sql = "SELECT ecovauserName,ecovauserage FROM ecovauserinfo WHERE ecovauserName = '" . $ecovauserName . "' and ecovauserage = '" . $ecovauserage . "'";
    $query = mysql_query($sql);

    if (mysql_num_rows($query) > 0) {
        $response['message'] = 'fail to post';
    } else {
        $insert = "INSERT INTO ecovauserinfo (ecovauserName,ecovauserage) VALUES ('" . $ecovauserName . "','" . $ecovauserage . "')";

        $query = mysql_query($insert);
        if ($query) {
            $response['message'] = 'EventFile Successfully stored';
            $response['success'] = true;
        } else {
            $response['message'] = 'Insert failed';
        }
    }
}
// The header is for telling that you are sending a json response
header('content-type: application/json; charset=utf-8');

// formatting a response as jsonp format.
echo $_GET['callback'] . '(' . json_encode($response) . ')';
在javascript中:

$.ajax({
    url: 'http://192.168.3.134:8080/ekova/postevent.php',
    type: 'POST',
    data: {ecovauserName: username, ecovauserage: password},
    contentType: "application/json; charset=utf-8",
    dataType: "jsonp",
    success: function (response) {
        alert(response.message);
    },
    error: function() {
        console.log('error');
    }
});

更多信息请访问

谢谢回复。我们如您所说进行了尝试,但当我们放置JSON时,数据未保存在数据库中。数据类型用于告诉ajax哪些数据需要返回,哪些数据不需要发送。因此,此更改不应影响您发布/保存的任何内容。另请注意>您告诉AJAX发布,但您正在检查$\u GET globals。您将需要使用$_POST['ecovauserName']和$_POST['ecovauserage']我们尝试删除jsonp。但是如果您将console.log(“blablabla”)放入success和fail中,则不调用success方法答案已编辑以反映。未调用成功函数控制台中的.log,但存储在db中的值请指导我如何调用成功您可以尝试将数据类型更改为“文本”吗?并在success和console.log中输入以下内容:返回警报(“测试”);我们放置了case1)数据类型:文本我们得到'in fail',case2)数据类型:json我们得到'in fail'case3)数据类型:jsonp我们得到'in fail',但只有case3数据保存到DBDanger:您正在使用,并且应该使用。您也容易受到现代API的影响,因为它会让您更容易使用。感谢您的回复,我们是PHP新手,请解释我们在哪里放置了两行标题('content-type:application/json;charset=utf-8');echo$\u GET['callback'].'(“.json_encode($response)。”);我们得到了错误:function(){console.log('error');}erroradd for debug
error:function(jqXHR,textStatus,errorhown){console.log(jqXHR);console.log(textStatus);console.log(errorhown);}
我们收到错误此对象parsererror错误:未调用jQuery11002985340391751379_1425647499106file:///D:/SaveApp/SaveApp/assets/ForLL/jquery.mobile-1.4.2.min.map加载资源失败:net::ERR_文件未找到
$.ajax({
    url: 'http://192.168.3.134:8080/ekova/postevent.php',
    type: 'POST',
    data: {ecovauserName: username, ecovauserage: password},
    contentType: "application/json; charset=utf-8",
    dataType: "jsonp",
    success: function (response) {
        alert(response.message);
    },
    error: function() {
        console.log('error');
    }
});