GroveStreams
ecoenghk private msg quote post Address this user
Hi, I use jquery ajax to do POST to update component description. Sometimes it works but sometimes not. Here is my code:
 
var postJson = {"component":{"description":"xxxxx"}};
$.ajax({
  method: "POST",
  url: "http://grovestreams.com/api/comp/xxx/property?api_key=xxxxxxxxxxxxx",
  dataType: "json",
  async: true,
  contentType: "application/json; charset=utf-8",
  data: JSON.stringify(postJson),
  crossDomain: true,   
  success: function (response) {
    alert(response.message); 
  },
  error: function (jqXHR, exception) {
    var msg = '';
    if (jqXHR.status === 0) {
       msg = 'Not connect.n Verify Network.';
    } else if (jqXHR.status == 404) {
       msg = 'Requested page not found. [404]';
    } else if (jqXHR.status == 500) {
       msg = 'Internal Server Error [500].';
    } else if (exception === 'parsererror') {
       msg = 'Requested JSON parse failed.';
    } else if (exception === 'timeout') {
       msg = 'Time out error.';
    } else if (exception === 'abort') {
       msg = 'Ajax request aborted.';
    } else {
       msg = 'Uncaught Error.n' + jqXHR.message;
     }
       alert(msg);
     }       
 }); 


When the request fail, I only got OPTION but there is no POST. And the console shows:
 
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://grovestreams.com/api/comp/105/property?api_key=d4d38dab-aabd-3bc0-a2be-c08ce3fdea0e. (Reason: missing token 'content-type' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel).
 

And I found there is no "content-type" in the OPTION request response header (access-control-allow-headers:"accept, origin, x-requested-with, co$".
When the request works the OPTION request response header is like this: (access-control-allow-headers:"accept, origin, x-requested-with, content-type".
Is there anything that can help at Grovestreams server side?
Thanks
Post 1 IP   flag post
MThomas private msg quote post Address this user
Where is your "Content-Length:"? I'm not too familiar with JQuery or AJAX. But for the HTTP part you usually have to send it a content length or sometimes it gets mad. I've seen it accept it though without a length.
Post 2 IP   flag post
MikeMills private msg quote post Address this user
If you're making the call from within a browser and you're browser is pointing at another domain besides GroveStreams.com, then you are making a Cross-domain call which most browsers restrict by default for security reasons. I think that might be your issue.

There are ways to work around that and I believe J-Query has some settings built into their ajax object to help with it. Search for J-Query and Cross-domain for solutions.
Post 3 IP   flag post
ecoenghk private msg quote post Address this user
Thanks, but why sometimes the post request works but sometimes not? I think that will block all request if it is the browser restriction problem.

Also, I use the jquery ajax to do PUT request and it works every time. So I suspect it is the problem of the POST.
Post 4 IP   flag post
MikeMills private msg quote post Address this user
There was a configuration typo for our web servers. Fixed it. Try it now and let me know if it works.
Post 5 IP   flag post
2968 5 5
Log in or sign up to compose a reply.