theadd() { if (this.addname == '' || this.addhosvalue == '') return this.$message.error('请填写完整信息') let data = { content: this.addname, paper_id: this.addhosvalue, qutype: this.radio, option1: this.option1, option2: this.option2, option3: this.option3, option4: this.option4, option5: this.option5, } this.$http.post(`admin/qu/questionAdd`, data).then(res => { if (res.data.code == 200) { this.$message.success('添加成功') this.getInfo() this.addutw = false } else { this.$message.error(res.data.msg) } }) },
时间: 2023-06-18 11:08:47 浏览: 53
这段代码是一个 Vue.js 中的方法,用于向服务器发送 POST 请求来添加一个问题。首先,它会检查 `addname` 和 `addhosvalue` 是否为空,如果有任何一个为空,则会弹出一个错误提示信息。然后,它会构造一个包含问题相关信息的数据对象 `data`,包括问题内容 `content`,所属试卷 ID `paper_id`,问题类型 `qutype`,以及选项 `option1` 到 `option5`。最后,它会使用 Vue.js 中的 `$http` 对象来发送 POST 请求,并根据服务器的响应结果弹出相应的提示信息。如果添加成功,则会刷新页面并关闭添加问题的窗口。
相关问题
这段代码改成可调用函数 : /*! * ASP.NET SignalR JavaScript Library 2.4.3 * http://signalr.net/ * * Copyright (c) .NET Foundation. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. * */ /// <reference path="....\SignalR.Client.JS\Scripts\jquery-1.6.4.js" /> /// <reference path="jquery.signalR.js" /> (function ($, window, undefined) { /// <param name="$" type="jQuery" /> "use strict"; if (typeof ($.signalR) !== "function") { throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js."); } var signalR = $.signalR; function makeProxyCallback(hub, callback) { return function () { // Call the client hub method callback.apply(hub, $.makeArray(arguments)); }; } function registerHubProxies(instance, shouldSubscribe) { var key, hub, memberKey, memberValue, subscriptionMethod; for (key in instance) { if (instance.hasOwnProperty(key)) { hub = instance[key]; if (!(hub.hubName)) { // Not a client hub continue; } if (shouldSubscribe) { // We want to subscribe to the hub events subscriptionMethod = hub.on; } else { // We want to unsubscribe from the hub events subscriptionMethod = hub.off; } // Loop through all members on the hub and find client hub functions to subscribe/unsubscribe for (memberKey in hub.client) { if (hub.client.hasOwnProperty(memberKey)) { memberValue = hub.client[memberKey]; if (!$.isFunction(memberValue)) { // Not a client hub function continue; } // Use the actual user-provided callback as the "identity" value for the registration. subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue), memberValue); } } } } } $.hubConnection.prototype.createHubProxies = function () { var proxies = {}; this.starting(function () { // Register the hub proxies as subscribed // (instance, shouldSubscribe) registerHubProxies(proxies, true); this._registerSubscribedHubs(); }).disconnected(function () { // Unsubscribe all hub proxies when we "disconnect". This is to ensure that we do not re-add functional call backs. // (instance, shouldSubscribe) registerHubProxies(proxies, false); }); proxies['realDataHub'] = this.createHubProxy('realDataHub'); proxies['realDataHub'].client = { }; proxies['realDataHub'].server = { send: function (name, message) { return proxies['realDataHub'].invoke.apply(proxies['realDataHub'], $.merge(["Send"], $.makeArray(arguments))); } }; return proxies; }; signalR.hub = $.hubConnection("/signalr", { useDefaultPath: false }); $.extend(signalR, signalR.hub.createHubProxies()); }(window.jQuery, window));
您可以将上述代码封装成一个可调用函数,例如:
```javascript
function connectToHub() {
if (typeof ($.signalR) !== "function") {
throw new Error("SignalR: SignalR is not loaded. Please ensure jquery.signalR-x.js is referenced before ~/signalr/js.");
}
var signalR = $.signalR;
var hubConnection = signalR.hubConnection("/signalr", { useDefaultPath: false });
var proxies = {};
function makeProxyCallback(hub, callback) {
return function () {
callback.apply(hub, $.makeArray(arguments));
};
}
function registerHubProxies(instance, shouldSubscribe) {
var key, hub, memberKey, memberValue, subscriptionMethod;
for (key in instance) {
if (instance.hasOwnProperty(key)) {
hub = instance[key];
if (!(hub.hubName)) {
continue;
}
if (shouldSubscribe) {
subscriptionMethod = hub.on;
} else {
subscriptionMethod = hub.off;
}
for (memberKey in hub.client) {
if (hub.client.hasOwnProperty(memberKey)) {
memberValue = hub.client[memberKey];
if (!$.isFunction(memberValue)) {
continue;
}
subscriptionMethod.call(hub, memberKey, makeProxyCallback(hub, memberValue), memberValue);
}
}
}
}
}
hubConnection.starting(function () {
registerHubProxies(proxies, true);
this._registerSubscribedHubs();
}).disconnected(function () {
registerHubProxies(proxies, false);
});
proxies['realDataHub'] = hubConnection.createHubProxy('realDataHub');
proxies['realDataHub'].client = {};
proxies['realDataHub'].server = {
send: function (name, message) {
return proxies['realDataHub'].invoke.apply(proxies['realDataHub'], $.merge(["Send"], $.makeArray(arguments)));
}
};
return proxies;
}
```
然后您可以在需要连接 SignalR 的地方调用该函数,例如:
```javascript
var hubProxies = connectToHub();
hubProxies.realDataHub.server.send('John', 'Hello!');
```
write java。The create method takes as arguments the name of a new library user, a number of books (as a string), and an integer representing the role of user to create (where the integer 0 means a lender and the integer 1 means a borrower). The create method of the controller then transforms the book number from a string to an integer (using the Integer.parseInt static method), creates an object from the correct class (based on the role specified by the user input: lender or borrower) and calls the addUser method of the library to add the new user object to the library. • If no exception occurs then the create method of the controller returns the empty string. • If the constructor of the Borrower class throws a NotALenderException then the create method of the controller must catch this exception and return as result the error message from the exception object. • If the parseInt method of the Integer class throws a NumberFormatException (because the user typed something which is not an integer) then the create method of the controller must catch this exception and return as result the error message from the exception object.
Here is a possible implementation of the `create` method in Java:
```java
public class LibraryController {
private Library library;
public LibraryController(Library library) {
this.library = library;
}
public String create(String name, String bookString, int role) {
try {
int bookCount = Integer.parseInt(bookString);
User user;
if (role == 0) {
user = new Lender(name, bookCount);
} else if (role == 1) {
user = new Borrower(name, bookCount);
} else {
return "Invalid role: " + role;
}
library.addUser(user);
return "";
} catch (NotALenderException e) {
return e.getMessage();
} catch (NumberFormatException e) {
return "Invalid book count: " + bookString;
}
}
}
```
In this implementation, the `create` method takes three arguments: the name of the new user, the book count as a string, and the role as an integer. The method first tries to parse the book count using `Integer.parseInt`, and if successful, creates a user object based on the role specified by the user input. If the role is not 0 or 1, an error message is returned.
If the user object is successfully created, the method calls the `addUser` method of the library to add the new user object to the library. If no exception occurs, an empty string is returned.
If the constructor of the `Borrower` class throws a `NotALenderException`, the method catches the exception and returns the error message from the exception object.
If the `parseInt` method of the `Integer` class throws a `NumberFormatException` (because the user typed something which is not an integer), the method catches the exception and returns an error message indicating that the book count is invalid.
相关推荐
![js](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)