简体   繁体   English

Coffeescript中的简单Ajax请求

[英]Simple Ajax request in Coffeescript

I have a small server running on localhost:8083 我有一个在本地主机上运行的小型服务器:8083

I tested a put request with Chrome postman client 我使用Chrome邮递员客户端测试了put请求

http://localhost:8083/addcompany?{"Ticker":"Hello", "Name":"Hello, Inc."}

which translates in 翻译成

POST /addcompany?{"Ticker":"Hello", "Name":"Hello, Inc."} HTTP/1.1
Host: localhost:8083
Cache-Control: no-cache

and correctly returns: 并正确返回:

{
"Ticker": "Hello",
"Name": "Hello, Inc."
}

I have tried to implement the same request in my client app. 我试图在客户端应用程序中实现相同的请求。

postTicker = (tkr, name) ->
    console.log "posting #{tkr}, #{name}"
    aCompany = JSON.stringify ({Ticker:tkr, Name: name})
    console.log aCompany
    queryUrl =  encodeURI 'http://localhost:8083/addcompany'
    $.ajax queryUrl,
        type: 'POST'
        timeout: 5000
        data: aCompany
        success: (response) ->
            #parsedResponse = $.parseJSON response
            alert response
            #removeAddBtn (parsedResponse.ticker)
        error: (response) ->
            console.log "AJAX Error: #{response}"

The request results in the error message: 该请求导致错误消息:

Uncaught TypeError: string is not a function 

for data: aCompany . data: aCompany

I believe you don't need to stringify your data. 我相信您不需要对数据进行分类处理。 Could you try to change the third line of your javascript: 您能否尝试更改JavaScript的第三行:

from : 来自

aCompany = JSON.stringify ({Ticker:tkr, Name: name})

to :

aCompany = {Ticker: tkr, Name: name}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM