0

This call is making 2 requests at the same time but while using postman only one is generated. If any1 can help will be greatly appreciated. it was though only sending one at first but now every time 2 requests are generated. Though wile placing alert in the comment is only showing once on the screen.

If there actual URL is needed I will again edit the question and place the real value.

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

sorry if I had made a silly mistake, please help me to sort that out

Ratish
  • 65
  • 3
  • 12
  • You cannot send `POST` data AND use `jsonp` as the dataType, because it appends some data to the querystring. Try it without using jsonp as the datatype. [See this post for more details](http://stackoverflow.com/questions/4508198/how-to-use-type-post-in-jsonp-ajax-call) – mikeyq6 Feb 15 '16 at 16:27
  • Cross-domain security prevents you from accessing the site from localhost. Maybe [this article](http://stackoverflow.com/questions/33820142/getting-request-doesnt-pass-access-control-check-no-access-control-allow-orig) can help – mikeyq6 Feb 16 '16 at 08:06

2 Answers2

0

Ok, there are a couple of things happening here that you need to fix.

Firstly,

You cannot send POST data AND use jsonp as the dataType, because it appends some data to the querystring, forcing the request to be sent as a GET request.

Either use json instead, or remove it altogether. Similarly with cache:true, it also will have the same effect. Either of these will cause the request to be sent as a GET request.

Try with this request:

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

Secondly, for the data that you are sending, you need to remove the single quotes around the parameters in the JSON.stringify call, ie this line:

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

Should becomd:

 data: JSON.stringify({u_source:"tarus", u_subject:Subject,u_detail:Detail,u_name:Name,u_email:Email,u_request:Request}),
mikeyq6
  • 1,138
  • 13
  • 27
0

try with something like that :) it works !

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']);
}
Themer
  • 584
  • 4
  • 9