使用JSON语法将参数作为值发送(send parametres as values using JSON syntax)

编程入门 行业动态 更新时间:2024-10-22 02:41:12
使用JSON语法将参数作为值发送(send parametres as values using JSON syntax)

我想发送一个带参数的网址,这些参数是一个带有javascript的表单所采用的值,我想用JSON来做,但是当我调试时我看到这个错误:未捕获的ReferenceError:名称未定义..

function recup() { var selectElmt = document.getElementById("name"); var selectcat = document.getElementById("msg"); var name = selectElmt.options[selectElmt.selectedIndex].value; var msg = selectcat.options[selectcat.selectedIndex].value; } function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg + $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }

调用值名称和消息时,这是一个语法错误,但我不知道如何修复它或在go函数中

i want to send an url with parametres, those parametres are values taken by a form with javascript and i want to use JSON to do it, but when i debug i see this error : Uncaught ReferenceError: name is not defined..

function recup() { var selectElmt = document.getElementById("name"); var selectcat = document.getElementById("msg"); var name = selectElmt.options[selectElmt.selectedIndex].value; var msg = selectcat.options[selectcat.selectedIndex].value; } function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg + $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }

it's a syntax error when calling the value name and msg but i don"t know how to fix it or in the go function

最满意答案

你有两个错误,关闭大括号和加号字符,代码应该是:

var msg = "hello"; // i just simplified the value var name = "test"; function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg; $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }

更新 :你需要创建名称和消息全局:

var name, msg; function recup() { var selectElmt = document.getElementById("name"); var selectcat = document.getElementById("msg"); name = selectElmt.options[selectElmt.selectedIndex].value; msg = selectcat.options[selectcat.selectedIndex].value; } function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg; $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }

并且recup需要在go之前go

You two errors, closing curly brace and plus character, the code shoud be:

var msg = "hello"; // i just simplified the value var name = "test"; function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg; $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }

UPDATE: You need to make name and msg global:

var name, msg; function recup() { var selectElmt = document.getElementById("name"); var selectcat = document.getElementById("msg"); name = selectElmt.options[selectElmt.selectedIndex].value; msg = selectcat.options[selectcat.selectedIndex].value; } function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg; $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }

and recup need to be executed before go

更多推荐

本文发布于:2023-07-04 17:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1026906.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   参数   JSON   send   syntax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!