2

I am trying to make a POST request in javascript. But response is not getting.

http://xecutehr.com/api/HRMSService.svc/PostAttendance?input={"Header":{"CompanyId":"MTF","LicenceKey":"MTF-4525"},"Body":{"E_Code":"01330","AttendanceDateTime":"2016-04-27T13:00:30","Mode":"I","DeviceId":"MTF1330"}}

This is the url format and this is the code I have written:

if(valid){
    var values    = form.serializeObject();
    var e_code    = values.staff_code.substring(3);
    var device_id = values.staff_code;
    var date_time = datetime;
    var url       = "http://xecutehr.com/api/HRMSService.svc/PostAttendance?input=";
    $.ajax({
        url:url,
        headers: {
            "CompanyId":"MTF",
            "LicenceKey":"MTF-160427-RHGB-4525"
        },
        type: "POST",
        data:{
            "E_Code":"0"+e_code,
            "AttendanceDateTime":date_time,
            "Mode":"I",
            "DeviceId":device_id
        },
        datatype: "jsonp",
        success: function(data) {
            alert(response); 
        }
    });
}

I tried a lot I am missing some basics in in . How can I handle this? suggest some solutions or links

acrosman
  • 12,814
  • 10
  • 39
  • 55
django
  • 203
  • 1
  • 4
  • 13
  • 1
    The endpoint you are trying to hit is not part of the same domain and is considered a Cross domain request. Check this out http://stackoverflow.com/questions/298745/how-do-i-send-a-cross-domain-post-request-via-javascript – Sushanth -- Apr 29 '16 at 18:42
  • @Uzbekjon what is this endpoint means. I am very new in this. Is there any solution out there? I am checking that link. – django Apr 29 '16 at 18:50
  • Is that endpoint intended to be integrated with a javascript client call? It either needs to have CORS configured to allow appropriate cross-domain access, or perhaps they have the ability to server jsonp response. Without one or the other, your request will never work. – Mike Brant Apr 29 '16 at 18:54

1 Answers1

3

You can't have jsonp and do a POST.

A jsonp request loads as a <script>.

E.g.

request: /url?callback=something&E_Code=0something

The response will be something like:

something({E_Code: '0something'})

Where something() is your ajax response handler.

notacouch
  • 3,657
  • 1
  • 19
  • 27
  • FIrst of all thanks for your valid reply. I am very new in this. I took 1 day for making his much.. If you can can you pls check my code and suggest what all changes needed and pls explain in simple words. – django Apr 29 '16 at 18:55
  • Follow @Susanth's link. If xecutehr.com's CORS (Cross-Origin Resource Sharing) settings allow access, follow point #2 in said link. If they don't, settings, for security reasons you cannot POST to them as it's a different domain. There's little else you can do about that if you go this route. If you need to POST, relying on a `
    ` instead of JavaScript ought to do it, see: http://stackoverflow.com/a/11425019/781824
    – notacouch Apr 29 '16 at 21:02