如何在nodejs中的URL中使用ajax从客户端发送的变量中接收值?

时间:2017-11-27 19:39:33

标签: javascript node.js ajax

我希望URL send server ajax delete id基于receive的产品NodeJS。我如何PHP api$sProductId = $_GET['id']; 一样var sUrl = "/delete-product" + sProductId; app.post('/delete-product' ???, upload.none(), (req, res) => { global.sUpdateProductImagePath = req.file.path.split("public/")[req.file.path.split("public").length - 1] user.deleteProduct(req.body, (err, jResult) => { if (err) { console.log(jResult) return res.send(jResult) } console.log(jResult) return res.send(jResult) }) }) 如此:

user.deleteProduct = (jProductData, fCallback) => {
    global.db.collection('products').deleteOne({ "_id": "" }, (err, jResult) => {
        if (err) {
            var jError = { "status": "error", "message": "ERROR -> deleteProduct -> user.js -> 001" }
            return fCallback(false, jError)
        }
        var jOk = { "status": "ok", "message": "user.js -> product deleted -> 000" }
        console.log(jResult)
        return fCallback(false, jOk)
    })
}

这是来自ajax调用的URL:

Private t As New System.Threading.Thread(AddressOf oto)

Private Sub startbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    t.Start("x")
End Sub

Private Sub stopbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
    t.Abort()
End Sub

Function oto(ByVal url As String)
    Try
        If CBool(InStr(LCase(anasayfa.WebBrowser1.Url.ToString), url)) = False Then : Return False : End If
        Dim sourcecode = anasayfa.WebBrowser1.Document
        Dim linkler = sourcecode.GetElementsByTagName("a")
        Dim i = 0 : Dim b = 0 : Dim l = 0
        If linkler.Count <= 0 Then : Return False : End If
        For Each kelime In kelimelistesi()
            i = 0
            For Each link As HtmlElement In linkler
                i = i + 1
                If Not link.GetAttribute("target") = "_blank" Then
                    If link.GetAttribute("onclick") = "" Then
                        If CBool(InStr(link.GetAttribute("href").ToString, kelime)) = True Then : b = i : Exit For : End If
                    End If
                End If
            Next
            If b > 0 Then : Exit For : End If
        Next
        If b > linkler.Count Then : Return False : End If
        If b > 0 Then
            l = b - 1
            anasayfa.ToolStripStatusLabel1.Invoke(Sub() anasayfa.ToolStripStatusLabel1.Text = "İlgili link bulundu : " & linkler.Item(l).InnerHtml.ToString())
            linkler.Item(l).InvokeMember("click")
            waitdocumentloading()
            Return True
        Else
            anasayfa.ToolStripStatusLabel1.Invoke(Sub() anasayfa.ToolStripStatusLabel1.Text = "İlgili link bulunamadı")
            Return False
        End If
    Catch ex As System.Threading.ThreadAbortException
        ' need to do something when aborting?
    End Try
    Return False
End Function

这是我的server.js

{{1}}

我的user.js:

{{1}}

2 个答案:

答案 0 :(得分:1)

如果你想在路径参数中使用id

,你可以这样做
list()

您还可以在搜索查询中发送ID,例如/ delete-product?id = yourid

compile "com.google.firebase:firebase-database:11.6.0"
compile 'com.google.firebase:firebase-auth:11.6.0'

答案 1 :(得分:0)

传递参数以将其从客户端发送到服务器的方式是get方法。 id是网址的一部分。

因此,在服务器中,您应该更改get的帖子并添加/:idproduct以接收它。

此外,您应该更改为req.params:

,而不是req.body

例如:

app.get('/ delete-product /:idproduct',(req,res)=&gt; {

    //global.sUpdateProductImagePath = req.file.path.split("public/")[req.file.path.split("public").length - 1]
    //req.body = req.params.idproduct
    user.deleteProduct(req.params.idproduct, (err, jResult) => {
        if (err) {
            console.log(jResult)
            return res.send(jResult)
        }
        console.log(jResult)
        return res.send(jResult)

    })
})