8

I create a method to post json data to web service :

 function WishList() { }
 WishList.prototype.addToWishList = function(redirectURL, postURL, userObj) {
    $.ajax({
           type: "POST",
           url: postURL,
           data: JSON.stringify(userObj),
           dataType: 'json',
           contentType: "application/json",
           success: function(data){alert(data);},
           failure: function(errMsg) {
              alert(errMsg);
           }
 }

 This is my object:

 var user1 = {
            ID:1,
            Sex:1,
            Name:"titi",
            Company:"ABC",
            Address:"Phnom Penh",
            Email:"test.abc@gmail.com",
            Phone:"011123456",
            WebAccount:"test.abc@gmail.com",
            Password:"123456",
            GroupCustomerID:125,
            Stars:1,
            IsVIP:0,
            PriceLevel:1,
            LastDateSale:"\/Date(-62135596800000)\/",
            TotalCredit:150.12,
            AgingData:null,
            TotalRedeemPoint:1000.00,
            RedeemData:null,
            ExchangeRate:155.00,
            HistoryData:null
        };          

 Calling function :

 $(document).ready(function () { 
    var myWishList = new WishList();
    $('#addToWishList').click(function(){
       myWishList.addToWishList('http://www.blahblahblah.com' , 'http://blahblah/Website/Products/Product.svc/Wishlist/' , user1);
    });
 });

Then I got errors in my console : "NetworkError: 405 Method Not Allowed in firefox and Invalid HTTP status code 405 , XMLHttpRequest cannot load url in chrome.

Note: When I use Rest Client of Chrome to POST to web service, it worked.

Any help would be much appreciated, thank you.

Nothing
  • 2,588
  • 11
  • 52
  • 109

1 Answers1

11

Not sure what you are using as the service on the other end but this may be due to cross domain posting. I hate to post a link and run but this may be of some use to you.

http://praneeth4victory.wordpress.com/2011/09/29/405-method-not-allowed/

Looks like they could get it working in IE but had some issues as you with the other browsers. Perhaps these couple changes will help access the service better.

This post was good at explaining the error and parts to it as well so if the above link is not helpful this one may help you diagnose the issue further.

http://www.checkupdown.com/status/E405.html

ok ok last edit, just wanted to make sure you have enough info to hopefully resolve your issue, here is a good article on the underlying problem I believe you are having..

http://www.d-mueller.de/blog/cross-domain-ajax-guide/

Tony
  • 3,199
  • 1
  • 25
  • 46
  • This issue was solved by enable POST in web service. – Nothing Oct 06 '13 at 09:25
  • I see the OP has POST, and the answer was to enable POST in ws. But, what if the service uses GET and the call in ajax uses GET? I have this same error. – Taersious Sep 24 '15 at 18:22
  • @Taersious, make sure your method has the `[HttpGet]` attribute, and make sure you are returning your json with something like this `return Json(data, JsonRequestBehavior.AllowGet)` – Tony Feb 09 '16 at 21:59