SORU
15 Temmuz 2012, Pazar


JSON 415 Desteklenmeyen medya türü ile başarısız POST, Spring mvc 3

Bir sunucu uygulaması için bir POST isteği göndermek için çalışıyorum. İsteği bu şekilde jQuery ile gönderilir:

var productCategory = new Object();
productCategory.idProductCategory = 1;
productCategory.description = "Descrizione2";
newCategory(productCategory);

nerede newCategory

function newCategory(productCategory)
{
  $.postJSON("ajax/newproductcategory", productCategory, function(
      idProductCategory)
  {
    console.debug("Inserted: "   idProductCategory);
  });
}

ve postJSON

$.postJSON = function(url, data, callback) {
    return jQuery.ajax({
    'type': 'POST',
    'url': url,
    'contentType': 'application/json',
    'data': JSON.stringify(data),
    'dataType': 'json',
    'success': callback
    });
};

Firebug ile bu JSON doğru gönderilir görüyorum

{"idProductCategory":1,"description":"Descrizione2"}

Ama 415 Desteklenmeyen medya türü. Kontrolör imzası Spring mvc

    @RequestMapping(value = "/ajax/newproductcategory", method = RequestMethod.POST)
public @ResponseBody
Integer newProductCategory(HttpServletRequest request,
        @RequestBody ProductCategory productCategory)

Birkaç gün önce çalışıyordu, şimdi değil. Daha fazla kod gerekirse göstereceğim. Teşekkürler

CEVAP
18 Temmuz 2012, ÇARŞAMBA


Bu daha önce başına gelmiş Bahar @ResponseBody ile geçirdim ve kabul Başlığı isteği ile gönderildi çünkü. Üstbilgi kümesi ile bir WordPress kullanmak için bir ağrı olabilir, ama bu benim için source kabul çalıştı

$.postJSON = function(url, data, callback) {
    return jQuery.ajax({
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    'type': 'POST',
    'url': url,
    'data': JSON.stringify(data),
    'dataType': 'json',
    'success': callback
    });
};

İçerik Türü Başlığı @veri isteği istemciden gönderilen belirlemek için RequestBody tarafından kullanılır. @İstemciye veri yanıt olarak gönderilen belirlemek için ResponseBody tarafından kullanılan başlık kabul. Hem başlıkları ihtiyacımız var.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • AndyMcMillinTV

    AndyMcMillin

    6 HAZİRAN 2007
  • BiGSH0TROB

    BiGSH0TROB

    7 NİSAN 2011
  • TopDJMag TV

    TopDJMag TV

    29 Temmuz 2010