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

3
  • 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 stackoverflow.com/questions/298745/… Commented Apr 29, 2016 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. Commented Apr 29, 2016 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. Commented Apr 29, 2016 at 18:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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 <form> instead of JavaScript ought to do it, see: stackoverflow.com/a/11425019/781824

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.