Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
Jquery 如果条件允许,针对不同url的Ajax调用uri_Jquery_Json_Ajax - Fatal编程技术网

Jquery 如果条件允许,针对不同url的Ajax调用uri

Jquery 如果条件允许,针对不同url的Ajax调用uri,jquery,json,ajax,Jquery,Json,Ajax,我试图根据json响应中的文本输入字段创建一个ajax调用帖子url,这里是我当前的ajax帖子 <script> $('#MyForm').submit(function(e){ var service_type2 = document.getElementById('type').value; if(service_type2=== 'post'){ $.ajax({ type: "POST",

我试图根据json响应中的文本输入字段创建一个ajax调用帖子url,这里是我当前的ajax帖子

<script>
    $('#MyForm').submit(function(e){
        var service_type2 = document.getElementById('type').value;
        if(service_type2=== 'post'){
          $.ajax({
          type: "POST",
          url: services/post.php,
          data: $(this).serialize(),
          success: success
        });
        }else{
          $.ajax({
          type: "POST",
          url: services/pre.php,
          data: $(this).serialize(),
          success: success
        });

        }
        .done(function(data){

            // show the response

            $('#success__params').html("You data will be saved");

        })
        .fail(function() {

            // just in case posting your form failed
             alert( "Posting failed." );

        });
        e.preventDefault();

        // to prevent refreshing the whole page page
        return false;

    });
</script>

$('#MyForm')。提交(函数(e){
var service_type2=document.getElementById('type').value;
如果(服务类型2==='post'){
$.ajax({
类型:“POST”,
url:services/post.php,
数据:$(this).serialize(),
成功:成功
});
}否则{
$.ajax({
类型:“POST”,
url:services/pre.php,
数据:$(this).serialize(),
成功:成功
});
}
.完成(功能(数据){
//显示响应
$('success_uuparams').html(“您的数据将被保存”);
})
.fail(函数(){
//以防你的表格发布失败
警报(“发布失败”);
});
e、 预防默认值();
//防止刷新整个页面
返回false;
});
如果#服务的输入文本值具有值响应“post”,则将执行第一个URL(反之亦然)

尝试使用按钮不起作用,我尝试了上面的代码,但页面保持刷新,有什么建议吗?我试着跟着这个,但还是不起作用


抱歉,英语不好:(.

您的变量名不匹配:

var service_type2 = document.getElementById('type').value;
if(service_type2 === 'post'){ // service_type2, not type
您必须将字符串(用引号括起来)传递给
url
属性:
url:'services/post.php'

并且
success
属性必须是函数

success: function(data){
  // show the response
  $('#success__params').html("You data will be saved");
},
您可能必须删除
.done()
fail()
并添加
error
属性

error: function() {
  // just in case posting your form failed
  alert( "Posting failed." );
}
最终代码:

<script>
    $('#MyForm').submit(function(e){
        e.preventDefault();
        var service_type2 = document.getElementById('type').value;
        if(service_type2 === 'post'){
          $.ajax({
            type: "POST",
            url: "services/post.php",
            data: $(this).serialize(),
            success: function(data){
              // show the response
              $('#success__params').html("You data will be saved");
            },
            error: function() {
              // just in case posting your form failed
              alert( "Posting failed." );
            }
          });
        }else{
          $.ajax({
            type: "POST",
            url: "services/pre.php",
            data: $(this).serialize(),
            success: function(data){
              // show the response
              $('#success__params').html("You data will be saved");
            },
            error: function() {
              // just in case posting your form failed
              alert( "Posting failed." );
            }
          });
        }
    });
</script>

$('#MyForm')。提交(函数(e){
e、 预防默认值();
var service_type2=document.getElementById('type').value;
如果(服务类型2==='post'){
$.ajax({
类型:“POST”,
url:“services/post.php”,
数据:$(this).serialize(),
成功:功能(数据){
//显示响应
$('success_uuparams').html(“您的数据将被保存”);
},
错误:函数(){
//以防你的表格发布失败
警报(“发布失败”);
}
});
}否则{
$.ajax({
类型:“POST”,
url:“services/pre.php”,
数据:$(this).serialize(),
成功:功能(数据){
//显示响应
$('success_uuparams').html(“您的数据将被保存”);
},
错误:函数(){
//以防你的表格发布失败
警报(“发布失败”);
}
});
}
});

Hi@fiter,我根据你的建议编辑了我的问题,但还是一样,重新加载页面,没有点击URI:(你使用的是哪个版本的jQuery?哇!!!伙计,非常感谢,它工作得很好。非常感谢,我已经为此挣扎了将近一周:(.非常感谢