user482375
user482375

Reputation:

JSON PUT Request Error

I am trying to use to a PUT call to a API using JSON. I am using jQuery and from what I can tell my code looks right:

<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            url: 'https://app.clickdimensions.com/Service.svc/v1/account/accountIdHere/capture',
            type: 'PUT',
            data: { 'reg_FirstName': 'First',
                    'reg_LastName': 'Last',
                    'reg_Phone': '123-342-1211',
                    'reg_Email': '[email protected]',
                    'reg_Company': 'My Company',
                    'reg_Address1': '123 Traffic Lane',
                    'reg_Address2': '',
                    'reg_City': 'Atlanta',
                    'reg_State': 'GA',
                    'reg_Zip': '12232',
                    'reg_Country': 'United States'  
            },
            success: function() { alert('PUT completed'); }
        });
    });
</script>

But when I run it I get the following error:

XMLHttpRequest cannot load https://app.clickdimensions.com/Service.svc/v1/account/accountIdHere/capture. Origin http://localhost:65116 is not allowed by Access-Control-Allow-Origin.

I've looked everywhere and tried about everything I can find to no avail. So I'm thinking my code might be wrong to do a JSON PUT call. Does my code look wrong? If so how could I fix it. If not, any idea on that error?

Thanks!

Upvotes: 0

Views: 811

Answers (2)

pete
pete

Reputation: 25081

Your jQuery looks good. The problem lies in Origin http://localhost:65116 is not allowed by Access-Control-Allow-Origin.

This suggests Cross-Domain resource loading, which is verboten in most situations. If you have control over https://app.clickdimensions.com/Service.svc/v1/account/accountIdHere/capture you can modify it to allow such requests from specific domains, or you could use JSONP instead of JSON to do cross-domain requests, although I'm unsure of your success with an HTTP PUT in such an instance. JSONP, to my knowledge, needs to be an HTTP GET.

Upvotes: 0

Kevin B
Kevin B

Reputation: 95020

Your code is correct, however it is not possible to do a PUT request cross-domain unless the domain you are requesting data from is returning the proper CORS headers.

Upvotes: 1

Related Questions