2

I'm using jQuery 1.10.1. I want to send POST request with content type set to application/json. I'm doing the following:

    $.ajax({
        type: "post",
        url: urlBase + "user/search",
        contentType: "application/json; charset=utf-8", 
        data: JSON.stringify(filter), 
        success: renderResponse,
        error: function(error) {
            console.log(error)
        }
    })

But the POST is not sent, and I get the following response on error callback:

Object { readyState=0, status=0, statusText="error"}

The OPTIONS requst is generated, but no POST follows. Here are the OPTIONS request and response:

Antwort-Header
Access-Control-Allow-Head...    X-Requested-With
Access-Control-Allow-Meth...    GET, POST, OPTIONS
Access-Control-Allow-Orig...    *
Access-Control-Max-Age  86400
Allow   GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS
Content-Length  0
Server  Jetty(6.1.1)

Anfrage-Header
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Access-Control-Request-He...    content-type
Access-Control-Request-Me...    POST
Cache-Control   no-cache
Connection  keep-alive
Host    localhost:8080
Origin  http://localhost
Pragma  no-cache
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0

What is a problem here? How can I make jQuery to set the content type header I want?

4

2 に答える 2

-1

これを試してください


var postData = JSON.stringify({"key": value});//this will be your json content

$.ajax({
        type: "post",
        url: urlBase + "user/search",
        contentType: "application/json; charset=utf-8", 
        data: postData,
dataType: "json," 
        success: renderResponse,
        error: function(error) {
            console.log(error)
        }
    })
于 2013-07-01T13:44:52.900 に答える