.
The first sentence is not difficult to understand. Delete the prompt message box. The second sentence cancels the default operation of mouse click. The last two sentences are very simple and will not be repeated. , you must know that newWidth has been mentioned before, indicating the width of mouse selection.
$.post(
opts.phpPath, //Use Ajax technology to send data to the server address
{ //Post past data
idBox : idBox,
rate : rate,
action : 'rating'
},
function(data) { //Callback function, mainly passes parameters to the plug-in custom function and executes it. .error)
{
if(opts.onSuccess) opts.onSuccess( element, rate );
}
else
{
if(opts.onError) opts.onError( element, rate );
}
},
'json' //Determine how to understand the returned data, it uses json.
);
Using jQuery to do Ajax is indeed very simple. Necessary comments have been made in the code. I won’t go into details here. The source code of this plug-in has been analyzed. It is relatively rough, but the whole logic may reflect some of it. I hope this study note can be useful to everyone. help. Now we enter the practical stage.
3. Practical jRating plug-in In order to be closer to the real application, we first use sql server to create a database table, which is an article type table with id, title, article content, and rating Four fields, screenshots are as follows:
The rating field defaults to -1, indicating that the article has not been rated yet. Of course, some people will now say that the design of this table is very unreasonable, because an article will not be rated only once. Every user should be able to comment. Yes, we are here just to demonstrate that the jRating plug-in uses Ajax for persistence. Since it is a demonstration operation, everything should be done frugally.
Create a new Web page to display the title, content and rating plug-in of the first article (id 1), see the front-end code:
The background CS code is as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
tempEntities cbx = new tempEntities(); //Used Entity Framework to obtain the data table
var page1 = cbx.jRatingArticles.Where(m => m.id == 1).SingleOrDefault();
page1_title.Text = page1.title;
page1_body.Text = page1.body;
}
}
In order to reduce the database connection code, I used Entity Framework, Only one table (jRatingArticle) is mapped, which is what we see above. Get the article object with id 1, and assign the corresponding properties to the Text property of the Label control.
The page effect is as follows:
We can see that in the JS code of the front page above, there is such a statement:
phpPath: 'tempAjax.aspx/UpdateComment '
It specifies the address to send data through Ajax when the mouse clicks on the plug-in. Here we use .net page technology to handle this asynchronous request. The background cs code of tempAjax.aspx is as follows:
[WebMethod( )]
public static void UpdateComment(int idBox, int rate)
{
tempEntities cbx = new tempEntities();
var page1 = cbx.jRatingArticles.Where(m => m.id == 1).SingleOrDefault();
page1.is_comment = rate;
cbx.SaveChanges();
}
At this time, we also need to modify the jRating plug-in In the original file, replace the $.post function in the mouse click (click) processing function as follows:
$.ajax({
type: "POST",
url: opts.phpPath,
data: '{"idBox":"' idBox '","rate": "' rate '"}',
contentType: "application/json; charset=utf-8",
dataType: "json"
});
Why? Change the source file because I want to change the contentType attribute of the Ajax request and send the request data in json format. The default is application/x-www-form-urlencoded
OK. Everything is ready. Let’s take a look at the execution effect (the selection ratio is 16 , 16 red stars):
Look at the changes in the database:
Test success! That’s it for today’s study. I hope this study note can be helpful to everyone!