Dealing with POST request

1,699 views
Skip to first unread message

Asutosh Dash

unread,
May 16, 2011, 7:53:04 AM5/16/11
to angular
Hi,

I have a web service which accepts parameter in the following format for POST request:

[{"acessType":"Internet","cancelReason":"Want to cancel","cntrctId":"CNT_4","contactEndDate":"2011-05-14T00:00:00+05:30","contactStartDate":"2010-03-01T00:00:00+05:30","contractTerm":"12","numberOfUser":"3","orderType":"cancel","paymentPlan":"Annual","productName":"ENDNOTE","productPrice":"2300"}] 

However when I give the same string in $xhr  like the following it fails,:
$xhr('POST','http://localhost:8080/anotherweb/resources/dbstore.tblcontract',[{"acessType":"Internet","cancelReason":"Want to cancel","cntrctId":"CNT_6","contactEndDate":"2011-05-14T00:00:00+05:30","contactStartDate":"2010-03-01T00:00:00+05:30","contractTerm":"12","numberOfUser":"3","orderType":"cancel","paymentPlan":"Annual","productName":"ENDNOTE","productPrice":"2300"}], function(code, response){
 

Could you explain what might be wrong.


Thanks in advance
Asutosh

Asutosh Dash

unread,
May 16, 2011, 10:42:39 AM5/16/11
to angular
Hi,

Just wanted to give more info on my problem:
I checked  in Firebug, it says "415 Unsupported Media Type".
Please suggest me how to solve this.

Regards
Asutosh

Witold Szczerba

unread,
May 16, 2011, 10:51:51 AM5/16/11
to ang...@googlegroups.com
This might be due to fixed 'Content-Type' request header, hard-coded in $xhr service.
I worked this around by changing that manually. Search for:

var XHR_HEADERS = {
  "Content-Type": "application/x-www-form-urlencoded",
  "Accept": "application/json, text/plain, */*",
  "X-Requested-With": "XMLHttpRequest"
};

and replace the: "application/x-www-form-urlencoded" to "application/json".

That should do the trick. The long-term solution is to wait for the new, more flexible, version of $xhr service.

Regards,
Witold Szczerba

2011/5/16 Asutosh Dash <pasu...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "angular" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Asutosh Dash

unread,
May 16, 2011, 10:56:10 AM5/16/11
to ang...@googlegroups.com
Thanks,

However, may I know where I have to search for this.

Regard
Asutsosh

Witold Szczerba

unread,
May 16, 2011, 11:00:04 AM5/16/11
to ang...@googlegroups.com
In angular.js file.

2011/5/16 Asutosh Dash <pasu...@gmail.com>

Asutosh Dash

unread,
May 16, 2011, 11:06:30 AM5/16/11
to ang...@googlegroups.com
Thanks,

I am using angular-0.9.12.js
In this file I searched for XHR_HEADERS, I did not find anything.
However I found this:


/**
   * @workInProgress
   * @ngdoc method
   * @name angular.service.$browser#xhr
   * @methodOf angular.service.$browser
   *
   * @param {string} method Requested method (get|post|put|delete|head|json)
   * @param {string} url Requested url
   * @param {string=} post Post data to send
   * @param {function(number, string)} callback Function that will be called on response
   *
   * @description
   * Send ajax request
   */
  self.xhr = function(method, url, post, callback) {
    if (isFunction(post)) {
      callback = post;
      post = _null;
    }
    outstandingRequestCount ++;
    if (lowercase(method) == 'json') {
      var callbackId = "angular_" + Math.random() + '_' + (idCounter++);
      callbackId = callbackId.replace(/\d\./, '');
      var script = document[0].createElement('script');
      script.type = 'text/javascript';
      script.src = url.replace('JSON_CALLBACK', callbackId);
      window[callbackId] = function(data){
        window[callbackId] = _undefined;
        completeOutstandingRequest(callback, 200, data);
      };
      body.append(script);
    } else {
      var xhr = new XHR();
      xhr.open(method, url, true);
      xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xhr.setRequestHeader("Accept", "application/json, text/plain, */*");
      xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
          completeOutstandingRequest(callback, xhr.status || 200, xhr.responseText);
        }
      };
      xhr.send(post || '');
    }
  };

Changing here, will it work?

Regards
Asutosh

Witold Szczerba

unread,
May 16, 2011, 12:02:34 PM5/16/11
to ang...@googlegroups.com
That would be it. Why are you using so ancient version? You are 3 releases behind. One day you will want/have to replace your angular with new version and you will face massive incompatibilities.

Vojta Jina

unread,
May 16, 2011, 12:06:23 PM5/16/11
to ang...@googlegroups.com
Yep, that's right. It has been extracted into XHR_HEADERS later on.
To be less hacky - you can rewrite just $xhr service instead of hacking angular.js.

We are going to rewrite $xhr so that you can access headers without hacks really soon...

V.

ashei...@gmail.com

unread,
May 24, 2012, 5:25:57 PM5/24/12
to ang...@googlegroups.com
Hello all,
I'm having the same issue Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers
Using the seed version, but i don't really know where to change this now.

Any help would be greatly appreciated.

Cheers,

Vojta Jína

unread,
Jun 12, 2012, 11:00:52 PM6/12/12
to ang...@googlegroups.com
Looks like you are doing cross domain request, you need to do xhr request to the same domain as your app, or use CORS.

V.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/6kZ8mgjZPx0J.

James Sawle

unread,
Aug 9, 2012, 9:23:00 AM8/9/12
to ang...@googlegroups.com
Hate to sound like a noob, but am new to Angular and trying to throw together something for a prototype and having the same issue with the Request header. Any example of how to use $xhr for cross-domain access would be much appreciated.
Reply all
Reply to author
Forward
0 new messages