Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Aug 2013
    Posts
    158
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Cross Domain JSON 'POST' request with Ajax

    *Sorry if this is the wrong area for this

    Hi,

    I want to do a pretty simple JSON POST request with ajax. However if I use JSON then I am unable to do the cross domain request and if I use JSONP then the POST request is not supported (I believe).
    What's the best way to do this?

    EDIT* POST request not PUT

    Code:
    function postLicense()
    {
      $.ajax
      ({
        url: "http://www.website.com",
        type: "POST",
        dataType: 'json',
        async: false,
        headers: {
          "Authorization": "Basic " + btoa("username"+ ":" +"password")
        },
        data: '{ "key":"1234-5678-9123", "purchaser":"Matthew", "licensedTo":"Company Name", "activationLimit":0, "products":[{"id":"a238-3619-454f9-937e-6436", "name": 4.0"}] }',
        success: function (responseData){
          var data = $.parseJSON(responseData);
          alert(data.key);
        },
        error: function ()
        {
          alert("error");
        }
      });
    }
    Last edited by 7daytheory; 08-18-2014 at 09:08 PM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Do you have access to the server in order to configure it?

    If not, then you only have the options to either

    1) use JSONP (with GET only) or

    2) run a proxy on your own server to send the POST request from this server to the remote one and output a copy of its response to the client

  • Users who have thanked devnull69 for this post:

    7daytheory (08-19-2014)

  • #3
    Regular Coder
    Join Date
    Aug 2013
    Posts
    158
    Thanks
    23
    Thanked 0 Times in 0 Posts
    I personally do not have access to configure the server however I am in contact with a third party who has set up the server.

    If I want to POST the data into a database on the server Im sending a request to I would have to POST correct?

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,412
    Thanks
    11
    Thanked 595 Times in 575 Posts
    I believe HTML5 added PUT to the method attribute of <form>, but i don't know how well-supported that is yet.

    using PUT in ajax via CORS would require a server config change (emmitting AccessControlAllowMethod header to white-list PUT.
    only GET and POST may be used without that custom header.
    you will also need to stop using the jsonp request type, use the form types that CORS allows without pre-flight.

    keep in mind that if for some reason you can't get ajax to work, you can actually use a regular old form to POST to any URL, the only limit is that JS can't see the response if it's off-site.
    Last edited by rnd me; 08-18-2014 at 08:24 PM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • Users who have thanked rnd me for this post:

    7daytheory (08-19-2014)

  • #5
    Regular Coder
    Join Date
    Aug 2013
    Posts
    158
    Thanks
    23
    Thanked 0 Times in 0 Posts
    I apologize I actually meant to put a POST request.

    long day

  • #6
    Regular Coder
    Join Date
    Aug 2013
    Posts
    158
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    I believe HTML5 added PUT to the method attribute of <form>, but i don't know how well-supported that is yet.

    using PUT in ajax via CORS would require a server config change (emmitting AccessControlAllowMethod header to white-list PUT.
    only GET and POST may be used without that custom header.
    you will also need to stop using the jsonp request type, use the form types that CORS allows without pre-flight.

    keep in mind that if for some reason you can't get ajax to work, you can actually use a regular old form to POST to any URL, the only limit is that JS can't see the response if it's off-site.
    Also I would put it into a basic HTML5 form however on submitting of the form I am running a ajax request to a php file that then sends back data to the ajax request and displays content on the page, and only if this is complete will it run the HTTP request cross domain.

    ..Actually as I was typing that I realized I can combine them all into one function and probably get that to work. Is there a different way to set headers to make it a json request.

    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •