没有正常工作ajax休息电话

时间:2016-02-15 14:48:33

标签: jquery ajax

此调用同时发出2个请求但使用邮递员时只生成一个请求。如果any1可以帮助将不胜感激。虽然最初只发送一个,但现在每次生成2个请求。虽然在评论中提示警告仅在屏幕上显示一次。

如果需要实际网址,我会再次编辑问题并放置真实值。

$(function(){
$("#form").on("submit", function(e){
    this.disabled = true;
    var Subject = $("input[name=Subject]").val();
    var Detail = $("textarea[name=Detail]").val();
    var Name = $("input[name=Name]").val();
    var Email = $("input[name=Email]").val();
    var Request = $( "#myselect option:selected" ).val();
    var data = JSON.stringify({u_source:"sssss", u_subject:Subject,u_description:Detail,u_name:Name,u_email:Email,u_category:Request})
    var settings = {
  "async": true,
  "crossDomain": true,
  "url": "something",
  "method": "POST",
  "headers": {
"accept": "application/json",
"content-type": "application/json",
"authorization": "Basic something",
"cache-control": "no-cache"
 },
 "processData": false,
 "data": data,
 "dataType": 'json'
}

$.ajax(settings).done(function (response) {
 alert('Thank you, Your feedback has been recorded');
 $('.overlay').hide();
 form.reset();
});
$.ajax(settings).fail(function (response) {
 alert('Sorry, Something went wrong. Please try again');
 $('.overlay').hide();
 form.reset();
});
  e.preventDefault(); 
  });
});
抱歉,如果我犯了一个愚蠢的错误,请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

好的,这里有一些你需要修复的事情。

首先,

  

您无法发送POST数据并使用jsonp作为dataType,因为它会将一些数据附加到查询字符串,从而强制请求作为GET请求发送。

使用json代替,或完全删除它。与cache:true类似,它也会产生相同的效果。这些中的任何一个都会导致请求作为GET请求发送。

尝试此请求:

$.ajax({
    url: "https://nespreprod.service-now.com/api/now/v1/table/u_incident_rest_inbound", //your api url is here
    //Host: "nespreprod.service-now.com",
    method: "POST",
    data: data,
    dataType: 'json',
    crossDomain: true,
    contentType: "application/json",
    accepts: "application/json",
    beforeSend: function(client) {
        //Authorization: "Basic " + btoa(username + ":" + password);
        client.setRequestHeader('Authorization', "Basic c3ZjLnJlc3QudHVyYXM6TjM1cmVzVEFQMQ==");
    },
    //Authorization: "Basic " + btoa(username + ":" + password),
    success: function(client, status, error) {
        alert('Email Send');
        //$('.overlay').hide();
    },
    error: function(client, status, error) {
        alert('Please Try again Later!');
        //$('.overlay').hide();;
    }
});

其次,对于您要发送的数据,您需要删除JSON.stringify调用中参数周围的单引号,即此行:

 data: JSON.stringify({"u_source":"tarus", 'u_subject':Subject,'u_detail':Detail,'u_name':Name,'u_email':Email,'u_request':Request}),

应该是becomd:

 data: JSON.stringify({u_source:"tarus", u_subject:Subject,u_detail:Detail,u_name:Name,u_email:Email,u_request:Request}),

答案 1 :(得分:0)

尝试类似的东西:)它有效!

string data='name:test & email:test@test';
     $.ajax({
        url: "restApi.php"
        method: "POST",
        data: data
        contentType: "application/json"



 }


 success: function(){
            alert('Email Send'); 

});

// Php Page

if((isset($_POST['name']))&&(isset($_POST['email'])))
{
    $name=$_POST['name'];
    $email=$_POST['email']);
}