AJAX POST请求正在向奇怪格式化的URL发送get请求

时间:2014-08-25 08:09:07

标签: jquery ajax http ember.js

我正在尝试使用以下代码在Ember中发送POST请求,但不管怎样,如果向指定的url端点发送POST请求,它会发送带有奇怪格式化URL的GET请求

App.UsersNewController = Ember.ArrayController.extend

  email: ''
  password: ''
  confirmPassword: ''
  errorMessage: ''

  actions:

      createUser: () ->
        @set('errorMessage', '')

        data = {}
        data.type = 'POST'
        data.url = '/api/users'
        data.email = @get('email')
        data.password = @get('password')
        data.confirmPassword = @get('confirmPassword')

        if data.password isnt data.confirmPassword
            alert('pass dont match') 

        data.dataType = 'json'
        data.contentType = 'application/json'
        data = JSON.stringify(data)

            Ember.$.ajax(data).success ((response) ->   
                console.log(response)
                ).error (xhr) ->
                        @set('errorMessage', xhr)
                        return

但不知何故,这会导致发送以下请求:

Remote Address:0.0.0.0:3000
Request URL:http://0.0.0.0:3000/%7B%22type%22:%22POST%22,%22url%22:%22/api/users%22,%22email%22:%22%22,%22password%22:%22%22,%22confirmPassword%22:%22%22,%22dataType%22:%22json%22,%22contentType%22:%22application/json%22%7D
Request Method:GET
Status Code:304 Not Modified
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,da;q=0.6
Connection:keep-alive
Cookie:connect.sid=s%3AanTd2zSeBOtIv2G_K8jIGaqI_qxutklX.bz7WjgA9h%2BtQ6z0mtyY4PFLZ8QgCfrfTAVErCbN3gi8
Host:0.0.0.0:3000
If-None-Match:W/"1bf-3541821583"
Referer:http://0.0.0.0:3000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36
X-Requested-With:XMLHttpRequest
Response Headersview source
Connection:keep-alive
Date:Sun, 24 Aug 2014 23:39:33 GMT
ETag:W/"1bf-3541821583"
set-cookie:connect.sid=s%3AvXDITZbNcIqVnjY7tMUhlJtOmAYIVwiF.9qKgAR7PeM6XVuTgk1krZ%2BT4wTGSioWKMWYaonqGU%2Bg; Path=/; HttpOnly
X-Powered-By:Express
ConsoleSearchEmulationRendering

1 个答案:

答案 0 :(得分:0)

URL,type,dataType和contentType不应该是数据的一部分!

data = {}
data .email = @get('email')
data .password = @get('password')
data .confirmPassword = @get('confirmPassword')

if data.password isnt data.confirmPassword
    alert('pass dont match') 

request= {}
request.dataType = 'json'
request.contentType = 'application/json'
request.type = 'POST'
request.url = '/api/users'
request.data = JSON.stringify(data)

Ember.$.ajax(request).success ((response) ->   
            console.log(response)
            ).error (xhr) ->
                    @set('errorMessage', xhr)
                    return