Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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 ajax rest调用工作不正常_Jquery_Ajax - Fatal编程技术网

Jquery ajax rest调用工作不正常

Jquery ajax rest调用工作不正常,jquery,ajax,Jquery,Ajax,此呼叫同时发出2个请求,但使用postman时只生成一个请求。如果有人能帮忙,我将不胜感激。起初只发送一个请求,但现在每次生成2个请求。虽然wile在评论中放置警告在屏幕上只显示一次 如果有实际的网址是必要的,我会再次编辑问题,并把真正的价值 $(function(){ $("#form").on("submit", function(e){ this.disabled = true; var Subject = $("input[name=Subject]").val();

此呼叫同时发出2个请求,但使用postman时只生成一个请求。如果有人能帮忙,我将不胜感激。起初只发送一个请求,但现在每次生成2个请求。虽然wile在评论中放置警告在屏幕上只显示一次

如果有实际的网址是必要的,我会再次编辑问题,并把真正的价值

$(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(); 
  });
});

对不起,如果我犯了一个愚蠢的错误,请帮我解决这个问题。好的,这里发生了一些事情,你需要解决

首先,


您不能发送
POST
数据并使用
jsonp
作为
dataType
,因为它会将一些数据附加到querystring,从而强制将请求作为
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}),
应注意:

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

试试这样的东西:)它很管用

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



 }


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

});
//Php页面

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

否。您应该保留所有实际字符串的引号,变量周围不应该有引号。请参见,它会生成正确的JSON字符串。您可以使用您正在使用的
HTML
更新您的问题吗?我已经更新了问题。你能帮忙吗。仍然找不到解决方案可能有效,但我需要以任何方式使其有效。您不能发送
POST
数据并使用
jsonp
作为数据类型,因为它会将一些数据附加到查询字符串中。不使用jsonp作为数据类型来尝试它。跨域安全性阻止您从本地主机访问站点。也许能帮上忙